How to catch PHP fatal errors (`E_ERROR`)?
P粉978551081
P粉978551081 2023-08-27 14:42:20
0
1
530
<p>I can use <code>set_error_handler()</code> to catch most PHP errors, but it does not work for fatal (<code>E_ERROR</code>) errors, such as calling a non-existent function. Is there any other way to catch these errors? </p> <p>I'm trying to resolve all errors by calling <code>mail()</code> and am running PHP 5.2.3. </p>
P粉978551081
P粉978551081

reply all(1)
P粉576184933

Use register_shutdown_function to log fatal errors, this 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 must 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 = "
    
表>";
return $content;
}


Use Swift Mailer to write the error_mail function.

See also:

Item Description
Error
$errstr
Error number
$errno
document $Error file
OK $Error line
track
$trace
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!