ThinkPHP5捕获致命错误流程
2023-03-29 23:03:03
195
{{single.collect_count}}

查询ThinkPHP5是怎么捕获致命错误。

首先全局搜索"register_shutdown_function",ThinkPHP5是通过register_shutdown_function方法注册程序执行结束时调用函数,找到项目路径下./thinkphp/library/think/Error.php文件使用了注册方法,找到的代码如下:

/** * 注册异常处理 * @access public * @return void */public static function register(){error_reporting(E_ALL);set_error_handler([__CLASS__, 'appError']);set_exception_handler([__CLASS__, 'appException']);register_shutdown_function([__CLASS__, 'appShutdown']);}

在该文件下找到方法appShutdown,找到的代码如下:

/** * 异常中止处理 * @access public * @return void */public static function appShutdown(){// 将错误信息托管至 think\ErrorExceptionif (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {self::appException(new ErrorException($error['type'], $error['message'], $error['file'], $error['line']));}// 写入日志Log::save();}

ThinkPHP5是通过register_shutdown_function方法注册结束时的函数回到,然后通过error_get_last方法获取错误信息

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}