[php-src] Issue #18864: ErrorException from error handler static utility method
| From: | uuf6429 | Date: | Mon, 16 Jun 2025 18:33:33 +0000 |
| Subject: | [php-src] Issue #18864: ErrorException from error handler static utility method | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250266@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/18864
Author: uuf6429
### Description
- since PHP is slowly transitioning from errors to exceptions
- since it's common for devs to want to handle errors and exception in a uniform way
- (also judging by such utility code in the php documentation)
It may be beneficial to implement a
ErrorException::handleError() (or similar) static
method, which can be fed directly to set_error_handler().
So essentially the [example
here](https://www.php.net/manual/en/class.errorexception.php#errorexception.examples) would be built
into the ErrorException class.
```php
// internal code
class ErrorException extends Exception
{
public static function handleError(int $errno, string $errstr, string $errfile, int $errline):
void
{
if (!(error_reporting() & $errno)) {
return;
}
if ($errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
return;
}
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
}
// userland code
set_error_handler(ErrorException::handleError(...)); // or array call or whatever
```
Implementation-wise I don't think this would cause any BC issues, nor should it be invasive.
Looking at [Symfony's error
handler](https://github.com/symfony/error-handler/blob/35b55b166f6752d6aaf21aa042fc5ed280fce235/ErrorHandler.php#L388),
there's a lot more going on in there, so this general approach probably won't cut it for
Symfony.
But there are otherwise many implementations that are similar to the example or simple/poorer: https://github.com/search?q=language%3APHP+set_error_handler+errorexception&type=code
(essentially this is what I'd like to tackle mainly)