Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shutdown callback is not invoked when calling undefined method

Tags:

php

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
like image 470
Josh Avatar asked Jul 12 '26 15:07

Josh


1 Answers

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:

  1. Non-fatal error is triggered (Undefined variable: undefined)
  2. User error handler is invoked
  3. Exception is thrown
  4. Fatal error is triggered (Call to a member function undefined() on a non-object)
  5. Shutdown function is not invoked, due to existing exception

The existing bug reports at https://bugs.php.net/61767 (with patch!) and https://bugs.php.net/60909 have additional details.

like image 99
Josh Avatar answered Jul 14 '26 06:07

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!