get_error_handler
Returns the callable currently registered as the error handler, or null for the built-in one.
get_error_handler(): ?callable- Added in PHP 8.5.0
Return value
?callableThe callable currently set as the error handler — the exact value that was passed to set_error_handler() — or null when the built-in handler is active.
Note that it cannot be used inside a running error handler to identify that handler: PHP unsets the active error handler while it executes, so get_error_handler() returns null there.
Example
When to use it
- Assert in a test that a library did not leave a stray error handler installed
- Skip registering your own handler when the application already has one
- Log which component owns error handling when debugging nested bootstrap code
Frequently asked questions
How do I check the current error handler in PHP before 8.5?
Call $old = set_error_handler(null) to read it, then set_error_handler($old) to put it back. It works but briefly leaves the built-in handler active, which is exactly the problem get_error_handler() solves.
Does get_error_handler return null when a handler is set?
Only if no user handler is active. Null means the built-in handler is in charge — either none was ever installed, or the last one was removed with restore_error_handler() or set_error_handler(null).
Can I call get_error_handler inside my own error handler?
You can call it, but it returns null: PHP unsets the active error handler for the duration of its own execution (to prevent recursion), exactly as it does for exception handlers since 8.3. Keep a reference to the handler in a variable if you need it inside itself. (Verified on PHP 8.5.9: get_error_handler() inside a running handler returns NULL.)
More examples
Install a handler only when there is none
The first call finds the built-in handler and installs the fallback, returning true. The second sees a non-null handler and backs off, so the application's existing error handling is never silently replaced.
<?php
function ensureHandler(callable $fallback): bool {
if (get_error_handler() !== null) {
return false; // something else already owns error handling
}
set_error_handler($fallback);
return true;
}
$fallback = fn(int $n, string $m): bool => print("fallback: {$m}\n");
var_dump(ensureHandler($fallback)); // true - we installed it
var_dump(ensureHandler($fallback)); // false - already set
restore_error_handler();Clean teardown that only restores what it set
Capturing the handler first and comparing afterwards means the restore only happens if the handler is still the one you installed. If the wrapped code replaced it, you avoid popping someone else's handler off the stack.
<?php
$before = get_error_handler();
set_error_handler(fn(int $n, string $m): bool => print("temp: {$m}\n"));
trigger_error('inside the temporary handler', E_USER_NOTICE);
if (get_error_handler() !== $before) {
restore_error_handler();
}
var_dump(get_error_handler() === $before);Identify a handler by shape
Because the exact callable comes back, an array callable can be unpacked into its object and method name. That is enough to log which class currently owns error handling, which a plain boolean check could never tell you.
<?php
class Reporter {
public function onError(int $no, string $msg): bool { echo "reported: {$msg}\n"; return true; }
}
set_error_handler([new Reporter(), 'onError']);
$current = get_error_handler();
echo is_array($current) ? get_class($current[0]) . '::' . $current[1] . "\n" : "closure\n";
trigger_error('from the object handler', E_USER_WARNING);
restore_error_handler();
Discussion