Catching PHP Fatal Errors
PHP's set_error_handler() often falls short when attempting to capture fatal errors (E_ERROR). This becomes especially problematic when working with non-existent function calls or other instances that trigger fatal errors.
To work around this limitation, you can utilize PHP's register_shutdown_function() for versions 5.2 . The following code snippet demonstrates its implementation:
register_shutdown_function("fatal_handler"); function fatal_handler() { $errfile = "unknown file"; $errstr = "shutdown"; $errno = E_CORE_ERROR; $errline = 0; $error = error_get_last(); if ($error !== NULL) { $errno = $error["type"]; $errfile = $error["file"]; $errline = $error["line"]; $errstr = $error["message"]; error_mail(format_error($errno, $errstr, $errfile, $errline)); } }
To complete the solution, define the error_mail and format_error functions. Here's an example:
function format_error($errno, $errstr, $errfile, $errline) { // Code to format and display error information goes here }
Consider employing Swift Mailer for the error_mail functionality.
For additional reference, explore the following resources:
The above is the detailed content of How Can I Catch PHP Fatal Errors Using `register_shutdown_function()`?. For more information, please follow other related articles on the PHP Chinese website!