Open mvorisek opened 10 months ago
The issue with this is that fatal errors are triggered when the engine may be in a state in which it's not safe to execute PHP code.
One alternative is to convert fatal errors to exceptions when possible, so that the engine can return to a safe state before the exception is handled. Many fatal errors were converted some times ago, but I suspect that the remaining ones are expensive or not worth to convert.
An other alternative would be expose the backtrace of the fatal error to the shutdown function (via a function similar to error_get_last()
, maybe), or other relevant information for an error handling use-case.
We could also delay handling of fatal errors until the shutdown stage, at which point it could be safer to execute user error handlers.
In general, even non-fatal errors are triggered from dangerous places, and cause all sorts of issues and complexities. It would be beneficial to delay execution of user error handlers until a safe execution point is reached, like we do for signals.
The issue with this is that fatal errors are triggered when the engine may be in a state in which it's not safe to execute PHP code.
In a state that does not allow exception to be caught (if it would be thrown), but code can be already executed thru register_shutdown_function
. So this issue is about to invoke set_error_handler
before the first registered function thru register_shutdown_function
is executed.
This makes sense. If we execute shutdown functions after a fatal error, we may as well execute error handlers at the same point of the program.
Description
Currently, fatal error can be handled, but it is not possible to do so using handler set by
set_error_handler
, but workaround usingregister_shutdown_function
. This is not only too complex, but there is no guarantee such handler is/was registered as the first shutdown function.An example is here: https://github.com/phpstan/phpstan-src/blob/1.10.50/src/Command/CommandHelper.php#L137-L154
This is a feature request to allow error handler to handle fatal error when php enters into shutdown before the 1st shutdown handler is executed.