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

前端 未结 17 2266
北荒
北荒 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 06:56

    I developed a way to catch all error types in PHP (almost all)! I have no sure about E_CORE_ERROR (I think will not works only for that error)! But, for other fatal errors (E_ERROR, E_PARSE, E_COMPILE...) works fine using only one error handler function! There goes my solution:

    Put this following code on your main file (index.php):

    ' . $typestr .
                ': ' . $errstr .
                ' in ' . $errfile .
                ' on line ' . $errline .
                '
    '; if(($errno & E_FATAL) && ENV === 'production'){ header('Location: 500.html'); header('Status: 500 Internal Server Error'); } if(!($errno & ERROR_REPORTING)) return; if(DISPLAY_ERRORS) printf('%s', $message); //Logging error on php file error log... if(LOG_ERRORS) error_log(strip_tags($message), 0); } ob_start(); @include 'content.php'; ob_end_flush(); ?>

提交回复
热议问题