Method for PHP to catch Fatal error errors

  • 2021-06-29 10:33:09
  • OfStack

Fatal error 1 generally does not need to be captured, but in a complex program, it is difficult to handle fatal error due to an accidental memory shortage.

For example, when fatal error came out of fetch in the MySQL class, it was difficult to locate the real problem.

PHP exception handling can be done via set_error_handler is used to capture. However, only errors at the NOTICE/WARNING level can be caught, for E_ERROR is powerless.

register_shutdown_function resolves set_error_Deficiencies of handler.

By registering the program end callback function with this function, you can catch errors that you would not normally catch. Then, you can use error_get_last judges errors. It's easy to find problems that are difficult to locate.


function shutdown_function()  
{  
    $e = error_get_last();    
    print_r($e);  
}
register_shutdown_function('shutdown_function');  


Related articles: