PHP 8.5: New get_exception_handler
and get_error_handler
functions
PHP allows setting custom error and exception handlers which get called when an error is triggered, or an exception is uncaught. It is also possible to restore the previous exception and error handlers.
However, until PHP 8.5, there was no direct way to get the currently set error and exception handlers. PHP 8.5 adds a couple of functions -- get_error_handler
and get_exception_handler
that provide this functionality.
Setting Error and Exception Handlers
set_error_handler
and set_exception_handler
functions accept a callable
parameter, that gets called when PHP emits an error or encounters an uncaught exception.
set_error_handler('my_custom_error_handler_function');
set_exception_handler('my_custom_exception_handler_function');
When setting error and exception handlers, the set_*_handler
function calls return the existing error handler.
New get_error_handler
function
PHP 8.5 adds a new function named get_error_handler
that returns the currently active error handler:
/**
* Returns the currently set error handler, or null if none is set.
* @return callable|null
*/
function get_error_handler(): ?callable {}
Usage examples
If there is no error handler set, calling get_error_handler
returns null
:
get_error_handler(); // null
When a custom error handler is set, get_error_handler
returns the set callable
:
set_error_handler(fn() => null);
get_error_handler(); // object(Closure)#1 (3) {...}
New get_exception_handler
function
The new get_exception_handler
function in PHP 8.5 works similarly to the get_error_handler
function. It returns the currently set exception handler, or null
if there is no exception handler set.
/**
* Returns the currently set exception handler, or null if none is set.
* @return callable|null
*/
function get_exception_handler(): ?callable {}
Usage examples
If there is no exception handler set, calling get_error_handler
returns null
:
get_error_handler(); // null
When a custom error handler is set, get_error_handler
returns the set callable
:
set_error_handler(fn() => null);
get_error_handler(); // object(Closure)#1 (3) {...}
PHP Polyfill
Because set_error_handler
and set_exception_handler
functions return the current handlers, it is possible to trivially polyfill these functions using PHP:
/**
* Returns the currently set error handler, or null if none is set.
* @return callable|null
*/
function get_error_handler(): ?callable {
$handler = set_error_handler(null);
restore_error_handler();
return $handler;
}
/**
* Returns the currently set exception handler, or null if is none set.
* @return callable|null
*/
function get_exception_handler(): ?callable {
$handler = set_exception_handler(null);
restore_exception_handler();
return $handler;
}
Backward Compatibility Impact
get_error_handler
and get_exception_handler
are new functions introduced in PHP 8.5. Unless an existing PHP application declares identical function names in the global namespace, this change has no backward compatibility impact.
Further, it is possible to polyfill these two functions in PHP.
get_error_handler
get_exception_handler
RFC Discussion Implementation