Handling Fatal PHP (E_ERROR) Errors
Problem:
If a fatal error occurs in a PHP script, such as calling a non-existent function, the error cannot be caught using the set_error_handler() function. How can you handle these critical errors?
Solution:
To catch fatal errors in PHP 5.2 , utilize the register_shutdown_function() function:
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)); } }
You need to implement the error_mail() and format_error() functions, for instance:
function format_error($errno, $errstr, $errfile, $errline) { $trace = print_r(debug_backtrace(false), true); $content = " <table> <thead><th>Item</th><th>Description</th></thead> <tbody> <tr> <th>Error</th> <td><pre class="brush:php;toolbar:false">$errstr
$errno
$trace
For sending emails, employ Swift Mailer to define the error_mail() function.
Additional Resources:
The above is the detailed content of How to Handle Fatal PHP Errors (E_ERROR) in PHP 5.2 ?. For more information, please follow other related articles on the PHP Chinese website!