rfc:get-error-exception-handler

This is an old revision of the document!


PHP RFC: Add get_error_handler(), get_exception_handler() functions

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 will return the currently defined error and exception handlers, respectively. If no custom handler is in place, they will return null.

The returned handler is the exact callback value that was passed to set_error_handler() or set_exception_handler().

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.

Add the pipe operator?
Real name Yes No
alcaeus  
alec  
asgrim  
beberlei  
bmajdak  
brzuchal  
bwoebi  
cmb  
cpriest  
crell  
cschneid  
daniels  
davey  
derick  
dragoonis  
duncan3dc  
edorian  
ericmann  
galvao  
girgias  
hirokawa  
ilutov  
john  
jwage  
kalle  
kguest  
kinncj  
levim  
mbeccati  
nicolasgrekas  
ocramius  
pollita  
ramsey  
reywob  
santiagolizardo  
sergey  
theodorejb  
thorstenr  
trowski  
weierophinney  
Final result: 33 7
This poll has been closed.

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Rejected Features

rfc/get-error-exception-handler.1739277946.txt.gz · Last modified: (external edit)