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

前端 未结 17 2267
北荒
北荒 2020-11-21 06:21

I can use set_error_handler() to catch most PHP errors, but it doesn\'t work for fatal (E_ERROR) errors, such as calling a function that doesn\'t e

17条回答
  •  情书的邮戳
    2020-11-21 07:02

    Well, it seems possible to catch fatal errors some other way :)

    ob_start('fatal_error_handler');
    
    function fatal_error_handler($buffer){
        $error = error_get_last();
        if($error['type'] == 1){
            // Type, message, file, line
            $newBuffer='
    Fatal Error
    • Line ' . $error['line'] . '
    • Message ' . $error['message'] . '
    • File ' . $error['file'] . '
    Back
    '; return $newBuffer; } return $buffer; }

提交回复
热议问题