What’s New in PHP 8.5: get_error_handler() and get_exception_handler() Explained

Author

Kritim Yantra

Apr 25, 2025

What’s New in PHP 8.5: get_error_handler() and get_exception_handler() Explained

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.


🧠 A Bit of Context

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:

  • Temporarily swapping handlers to capture the previous one
  • Parsing output from phpinfo()
  • Relying on polyfills or libraries like Symfony

Now, with get_error_handler() and get_exception_handler(), you can directly retrieve the current handlers. No more hacks—just clean, modern PHP.


🆕 Introducing the New Functions

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.


👀 Basic Usage Example

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.


🔁 Replacing Old Workarounds

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.


🔗 Chaining Error Handlers — A Real Example

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:

  • Writes to error.log
  • Displays [Primary] Something went wrong!

🔁 Chaining Exception Handlers

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!


🔧 Why This Matters

✅ For Frameworks & Libraries

Safely add, wrap, or override handlers without clobbering others. Ideal for logging, debugging, middleware, etc.

✅ For Debugging & Auditing

Find out if another part of your system has silently changed the global handler.

✅ For Clean Code

No more dirty hacks or temporary handler swaps. Your codebase just got cleaner and more readable.


📌 Developer Tips

  • Use restore_error_handler() and restore_exception_handler() to revert back after overrides.
  • Always check if get_*_handler() returns null. It means no user-defined handler is active.
  • These additions eliminate the need for polyfills, like those in Symfony or Laravel ecosystems.

✅ Final Thoughts

PHP 8.5 brings a quiet but powerful update for developers: clear visibility and control over your runtime handlers.

You can now:

  • Dynamically inspect current error/exception handlers
  • Chain or wrap handlers without breaking previous logic
  • Simplify debugging and logging tools
  • Avoid dirty workarounds

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.

Tags

Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts