PHP Error Listener
Join the DZone community and get the full member experience.
Join For FreeError 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();
}
}
PHP
Opinions expressed by DZone contributors are their own.
Comments