The following code works as expected: throws the ErrorException and calls the shutdown function for the fatal-error generated by require
register_shutdown_function(function() {
echo "anyway, hello worldn";
});
set_error_handler(function($severity, $message, $file, $line) {
throw new ErrorException($message, 0, $severity, $file, $line);
});
set_exception_handler(function($exception) {
echo $exception->getMessage().PHP_EOL;
});
require "unavailable_file";
Output:
require(unavailable_file): Failed to open stream: No such file or directory
anyway, hello world
But fatal error generated by named arguments fails to call the exception-handler and the shutdown function
// replacing require in the previous code with the following
function foo() {}
foo(...[], bar: "baz");
Output:
Fatal error: Cannot combine named arguments and argument unpacking
Combining all of them also is not working as expected and the ErrorException
from require
is not caught
// ...
require "unavailable_file";
function foo() {}
foo(...[], bar: "baz");
Output:
Fatal error: Cannot combine named arguments and argument unpacking
So is this another bug or am I missing something here?
PS: The PHP version is 8.0.0RC2 (cli)
2
Answers
As pointed out in the comments, it was indeed the case of different fatal type of fatal errors.
Since this falls under the category "generated before script is executed", unfortunately the shutdown function will never be called. Too bad I did’t find anything showing which fatal errors fall under this category.
Yes, it turns out this is not a bug!
Errors of type
E_COMPILE_ERROR
And also
E_PARSE
Are the only reason for such behavior (plus if the process is killed with a SIGTERM, or SIGKILL signal or
exit
ordie
were called before the shutdown function)Here is a list of such errors
private
abtract methods