Over a million developers have joined DZone.

PHP Error Listener

·
Error listener for PHP, shows simple errors as exceptions.


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
class ErrorListener{
	static protected $listening = false;
	static protected $listeningExceptions = false;

	public static function start(){
		if(self::$listening)
			return;
		set_error_handler(array('ErrorListener', 'dispatcher'));
		self::$listening = true;
	}
	public static function stop(){
		if(!self::$listening)
			return;
		restore_error_handler();
		self::$listening = false;
	}
	public static function dispatcher($code, $message){
		throw new Exception($message, $code);
	}
	public static function setDefaultExceptionHandler($callback){
		if(self::$listeningExceptions)
			return;
		set_exception_handler($callback);
	}
	public static function restoreDefaultExceptionHandler(){
		if(!self::$listeningExceptions)
			return;
		restore_exception_handler();
	}
}
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}