Add get_error_handler(), get_exception_handler() functions View as Markdown

Implemented (PHP 8.5)
Authors
Arnaud Le Blanc, arnaud.lb@gmail.com
Date
2025-02-11
Version
0.9
Target
PHP 8.5
Source
wiki.php.net/rfc/get-error-exception-handler

Vote

Add get_error_handler(), get_exception_handler() functions? closed
Yes
28
No
0
28 voters
alcaeus Yes 2025-03-07 10:49
asgrim Yes 2025-03-05 19:37
ashnazg Yes 2025-03-05 15:19
ayesh Yes 2025-03-05 12:00
bwoebi Yes 2025-03-14 14:46
cpriest Yes 2025-03-14 15:41
crell Yes 2025-03-05 18:39
derick Yes 2025-03-09 15:47
fabpot Yes 2025-03-18 15:39
galvao Yes 2025-03-05 19:48
girgias Yes 2025-03-05 19:10
heiglandreas Yes 2025-03-16 07:48
jimw Yes 2025-03-09 02:25
josh Yes 2025-03-18 19:21
kalle Yes 2025-03-05 09:53
kguest Yes 2025-03-12 14:00
lbarnaud Yes 2025-03-05 09:40
malferov Yes 2025-03-07 05:00
mbeccati Yes 2025-03-06 11:25
mcmic Yes 2025-03-11 09:32
nicolasgrekas Yes 2025-03-05 10:43
ramsey Yes 2025-03-18 15:34
reywob Yes 2025-03-06 10:09
sebastian Yes 2025-03-05 10:32
sergey Yes 2025-03-05 09:54
theodorejb Yes 2025-03-05 15:03
timwolla Yes 2025-03-05 10:15
weierophinney Yes 2025-03-13 15:25

Discussions on the mailing list

[RFC] Add get_error_handler(), get_exception_handler() functions
by Arnaud Le Blanc · 2 messages
[VOTE] Add get_error_handler(), get_exception_handler() functions
by Arnaud Le Blanc · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

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.

Voting started on 2025-03-05 and will end on 2025-03-20 00:00:00 UTC.

Vote: Add get_error_handler(), get_exception_handler() functions? — Yes: 28 · No: 0 (closed)

Patches and Tests

https://github.com/php/php-src/pull/17693

Implementation

References

https://github.com/symfony/symfony/pull/58372#discussion_r1773407604

Rejected Features