rfc:get-error-exception-handler

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 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.

Add get_error_handler(), get_exception_handler() functions?
Real name Yes No
Final result: 0 0
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.txt · Last modified: 2025/02/11 13:06 by lbarnaud