Kritim Yantra
Apr 25, 2025
PHP 8.5 introduces two game-changing functions: get_error_handler()
and get_exception_handler()
. These new additions give developers something they've long needed—the ability to retrieve the currently active error and exception handlers at runtime, without resorting to hacks, phpinfo()
, or temporary overrides.
Let’s dive into what these functions are, why they matter, and how you can use them to level up your PHP error-handling strategy.
Until PHP 8.5, you could easily set error and exception handlers using:
set_error_handler()
set_exception_handler()
But what if you wanted to check what was already set? You’d have to use awkward workarounds, like:
phpinfo()
Now, with get_error_handler()
and get_exception_handler()
, you can directly retrieve the current handlers. No more hacks—just clean, modern PHP.
get_error_handler(): ?callable
Returns the currently active error handler callable—or null
if PHP's default is in use.
👉 PHP Manual: get_error_handler()
get_exception_handler(): ?callable
Returns the currently active exception handler—or null
if no custom handler is set.
👉 PHP Manual: get_exception_handler()
These are perfect for libraries, frameworks, logging tools, and anyone who needs dynamic insight into PHP’s error handling behavior.
Want to know what handlers are currently active? Here you go:
<?php
var_dump(get_error_handler()); // ?callable or null
var_dump(get_exception_handler()); // ?callable or null
✅ Clean. Simple. Reliable.
Previously, devs often did this:
$prev = set_error_handler($newHandler);
// Do something
restore_error_handler(); // Back to $prev
Now, you can just use:
$prev = get_error_handler();
It simplifies chaining, inspecting, and restoring handlers tremendously.
Let’s say you want to add logging to your current error handler without removing it:
<?php
function primaryErrorHandler(int $errno, string $errstr): void {
echo "[Primary] $errstr\n";
}
set_error_handler('primaryErrorHandler');
// Wrap it with logging
$previous = get_error_handler();
set_error_handler(function($errno, $errstr, $errfile, $errline) use ($previous) {
file_put_contents(__DIR__.'/error.log', "$errstr\n", FILE_APPEND);
if ($previous) {
call_user_func($previous, $errno, $errstr, $errfile, $errline);
}
});
trigger_error("Something went wrong!", E_USER_WARNING);
📝 Output:
error.log
[Primary] Something went wrong!
Same idea, now for exceptions:
<?php
function primaryExceptionHandler(Throwable $ex): void {
echo "[PrimaryEx] {$ex->getMessage()}\n";
}
set_exception_handler('primaryExceptionHandler');
// Wrap with notification
$prevEx = get_exception_handler();
set_exception_handler(function(Throwable $ex) use ($prevEx) {
mail('admin@example.com', 'Unhandled Exception', $ex->getMessage());
if ($prevEx) {
$prevEx($ex);
}
});
throw new Exception("Oops, something bad happened!");
📬 Result: Admin gets an email, and your original handler still runs. Win-win!
Safely add, wrap, or override handlers without clobbering others. Ideal for logging, debugging, middleware, etc.
Find out if another part of your system has silently changed the global handler.
No more dirty hacks or temporary handler swaps. Your codebase just got cleaner and more readable.
restore_error_handler()
and restore_exception_handler()
to revert back after overrides.get_*_handler()
returns null
. It means no user-defined handler is active.PHP 8.5 brings a quiet but powerful update for developers: clear visibility and control over your runtime handlers.
You can now:
It’s a small addition with a huge quality-of-life boost, especially for complex applications, custom logging systems, and framework-level tools.
🔧 Start using get_error_handler()
and get_exception_handler()
today—and write more predictable, maintainable error-handling code.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google