How do I catch a PHP fatal (`E_ERROR`) error? How do I catch a PHP fatal (`E_ERROR`) error? php php

How do I catch a PHP fatal (`E_ERROR`) error?


Log fatal errors using the register_shutdown_function, which requires PHP 5.2+:

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 will have to define the error_mail and format_error functions. For example:

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>$errstr</pre></td>            </tr>            <tr>                <th>Errno</th>                <td><pre>$errno</pre></td>            </tr>            <tr>                <th>File</th>                <td>$errfile</td>            </tr>            <tr>                <th>Line</th>                <td>$errline</td>            </tr>            <tr>                <th>Trace</th>                <td><pre>$trace</pre></td>            </tr>        </tbody>    </table>";    return $content;}

Use Swift Mailer to write the error_mail function.

See also:


I just came up with this solution (PHP 5.2.0+):

function shutDownFunction() {    $error = error_get_last();     // Fatal error, E_ERROR === 1    if ($error['type'] === E_ERROR) {         // Do your stuff    }}register_shutdown_function('shutDownFunction');

Different error types are defined at Predefined Constants.


PHP doesn't provide conventional means for catching and recovering from fatal errors. This is because processing should not typically be recovered after a fatal error. String matching an output buffer (as suggested by the original post the technique described on PHP.net) is definitely ill-advised. It's simply unreliable.

Calling the mail() function from within an error handler method prove to be problematic, too. If you had a lot of errors, your mail server would be loaded with work, and you could find yourself with a gnarly inbox. To avoid this, you might consider running a cron to scan error logs periodically and send notifications accordingly. You might also like to look into system monitoring software, such as Nagios.


To speak to the bit about registering a shutdown function:

It's true that you can register a shutdown function, and that's a good answer.

The point here is that we typically shouldn't try to recover from fatal errors, especially not by using a regular expression against your output buffer. I was responding to the accepted answer, which linked to a suggestion on php.net which has since been changed or removed.

That suggestion was to use a regex against the output buffer during exception handling, and in the case of a fatal error (detected by the matching against whatever configured error text you might be expecting), try to do some sort of recovery or continued processing. That would not be a recommended practice (I believe that's why I can't find the original suggestion, too. I'm either overlooking it, or the php community shot it down).

It might be worth noting that the more recent versions of PHP (around 5.1) seem to call the shutdown function earlier, before the output buffering callback is envoked. In version 5 and earlier, that order was the reverse (the output buffering callback was followed by the shutdown function). Also, since about 5.0.5 (which is much earlier than the questioner's version 5.2.3), objects are unloaded well before a registered shutdown function is called, so you won't be able to rely on your in-memory objects to do much of anything.

So registering a shutdown function is fine, but the sort of tasks that ought to be performed by a shutdown function are probably limited to a handful of gentle shutdown procedures.

The key take-away here is just some words of wisdom for anyone who stumbles upon this question and sees the advice in the originally accepted answer. Don't regex your output buffer.