====== PHP RFC: Add get_error_handler(), get_exception_handler() functions ======
* Version: 0.9
* Date: 2025-02-11
* Author: Arnaud Le Blanc, arnaud.lb@gmail.com
* Status: Draft
* First Published at: http://wiki.php.net/rfc/get-error-exception-handler
===== Introduction =====
PHP does not provide a direct way to fetch the currently registered error or exception handlers. Users who need to inspect these handlers must resort to a workaround that temporarily sets a new handler and then immediately restores the previous one:
$current_error_handler = set_error_handler('valid_callback');
restore_error_handler();
This workaround is cumbersome and error-prone.
===== Proposal =====
This RFC proposes two new functions that allow to query the current error/exception handlers in a more direct way:
* function get_error_handler(): ?callable
* function get_exception_handler(): ?callable
The functions return the currently defined error and exception handlers, respectively. If no handler is in place, null is returned.
The returned handler is the exact callback value that was passed to ''set_error_handler()'' or ''set_exception_handler()'' to define it.
==== Examples ====
set_error_handler(null);
get_error_handler(); // null
$handler = [$this, 'error_handler'];
set_error_handler($handler);
get_error_handler() === $handler; // true
$new_handler = $this->error_handler(...);
$old_handler = set_error_handler($new_handler);
get_error_handler() === $new_handler; // true
restore_error_handler();
get_error_handler() === $old_handler; // true
===== Backward Incompatible Changes =====
The function names get_error_handler, get_exception_handler will no longer be available within the global namespace.
===== Proposed PHP Version(s) =====
Next 8.x.
===== Proposed Voting Choices =====
Yes or no vote. 2/3 required to pass.
* Yes
* No
===== Patches and Tests =====
https://github.com/php/php-src/pull/17693
===== Implementation =====
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
===== References =====
https://github.com/symfony/symfony/pull/58372#discussion_r1773407604
===== Rejected Features =====