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:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}