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

前端 未结 17 2580
北荒
北荒 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:07

    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.

提交回复
热议问题