I am trying to write an error handler class for php. I've tested that the object has been created but it doesn't seem to be handling errors.
I used the same code in a function in the index file initially and it worked fine, but I'd rather have a class. Why is this not handling errors?
class class_error
{
public function __construct()
{
// set to the user defined error handler
set_error_handler($this->errorHandler());
}
public function errorHandler($errno, $errstr, $errfile, $errline)
{
//don't display error if no error number
if (!(error_reporting() & $errno)) {
return;
}
//display errors according to the error number
switch ($errno)
{
case E_USER_ERROR:
echo "<b>ERROR</b> [$errno] $errstr<br />
";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
";
echo "Aborting...<br />
";
exit(1);
break;
case E_USER_WARNING:
echo "<b>WARNING</b> [$errno] $errstr<br />
";
break;
case E_USER_NOTICE:
echo "<b>NOTICE</b> [$errno] $errstr<br />
";
break;
default:
echo "<b>UNKNOWN ERROR</b> [$errno] $errstr<br />
";
break;
}
//don't execute PHP internal error handler
return true;
}
}
Use this:
set_error_handler(array($this, "errorHandler"));
More info: http://php.net/manual/en/language.types.callable.php