Inspecting Handlers

Inspecting Handlers with get_error_handler and get_exception_handler

PHP 8.5 added get_error_handler() and get_exception_handler(), which return the active callback as registered, or null. Before, you had to set a dummy handler, keep the one it returned and restore it. Frameworks and test tools use the getters to wrap the current handler instead of replacing it.

A library wraps the application's error handler instead of replacing itPHP
<?php
set_error_handler(fn(int $no, string $msg) => print "app logged: $msg\n");
$inner = get_error_handler();                   // a library wraps whatever is active
set_error_handler(function (...$args) use ($inner) {
    echo 'library saw it; ';
    return $inner(...$args);
});
$qty = [];
$qty = $qty['BK-101'];
Output
library saw it; app logged: Undefined array key "BK-101"

A test suite can compare get_error_handler() before and after each test to catch a test that forgot to restore its handler.