DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP Error Listener

11.17.2008
| 4230 views |
  • submit to reddit
        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();
	}
}