I’ve trying to use this:
$error_handler = function($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
};
$exception_handler = function($exception) {
self::invokeHttpError(500);
};
set_error_handler($error_handler, E_ALL | E_STRICT);
set_exception_handler($exception_handler);
However, it fails when I call this:
$fn = function() {
$application->test(); // $application is undefined
};
$fn();
The error handler is called, but not the exception handler. What is happening?
Dereferencing an undefined object results in a fatal and uncatchable error. That’s why your exception handler and error handler are not called when this happens.
This behaviour really irritates me as a developer though, I wish it would get turned into a catchable exception instead.
Update
One thing you could do to curb this problem is to throw an ErrorException inside your regular error handler. This would prevent an uncatchable fatal error when dereferencing an undefined symbol.