Consider the following two PHP (5.4) scripts. Why is the callback passed to register_shutdown_function
only invoked when script A is executed, but not when script B is executed?
Script A
set_error_handler(function() {
throw new Exception();
});
register_shutdown_function(function() {
echo "shutdown handler invoked\n";
});
undefined();
// "shutdown handler invoked" IS displayed
Script B
set_error_handler(function() {
throw new Exception();
});
register_shutdown_function(function() {
echo "shutdown handler invoked\n";
});
$undefined->undefined();
// "shutdown handler invoked" IS NOT displayed
It's a bug—if the callable registered with set_error_handler
throws an exception, the shutdown function will not be invoked.
In this particular case, the following chain of events happens:
The existing bug reports at https://bugs.php.net/61767 (with patch!) and https://bugs.php.net/60909 have additional details.
Here is what I found out.
register_shutdown_function is called upon exit.
$undefined->undefined(); triggers E_NOTICE error because $undefined is undefined as a variable which goes through set_error_handler. if you call throw new Exception, php for some reason doesn't trigger register_shutdown_function handler when exiting just crashes, if you want to throw an exception maybe you can call your exception function directly.
If you make a dummy class and define $undefined, but doesn't make a member function called undefined, it will be behave in the same way as calling undefined() as below
undefined(); triggers E_ERROR which immediately exits triggering shutdown_function handler
You could do this in the shutdown function beside using error handler to catch different types of errors:
function shutdown_function_handler(){
if ($error = error_get_last()){
switch ($error['type']){
case E_ERROR:
case E_USER_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
echo " ERRROR:".$error['message'];
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.