Handle fatal errors in PHP using register_shutdown_function()(使用 register_shutdown_function() 处理 PHP 中的致命错误)
问题描述
根据 对此答案的评论 可以通过 set_error_handler() 捕获的noreferrer">关闭函数.
According to the comment on this answer it is possible to catch Fatal Errors through a shutdown function which cannot be caught using set_error_handler().
但是,我无法确定如何确定关闭是由于致命错误还是由于脚本结束.
However, I couldn't find out how to determine if the shutdown has occured due to a fatal error or due to the script reaching its end.
此外,调试回溯功能似乎在关闭功能中已失效,因此记录发生致命错误的堆栈跟踪变得毫无价值.
Additionally, the debug backtrace functions seem to be defunct in the shutdown function, making it pretty worthless for logging the stack trace where the Fatal Error occured.
所以我的问题是:在保持创建适当回溯的能力的同时,对致命错误(尤其是未定义的函数调用)做出反应的最佳方法是什么?
So my question is: what's the best way to react on Fatal Errors (especially undefined function calls) while keeping the ability to create a proper backtrace?
推荐答案
这对我有用:
function shutdown() {
$error = error_get_last();
if ($error['type'] === E_ERROR) {
// fatal error has occured
}
}
register_shutdown_function('shutdown');
spl_autoload_register('foo');
// throws a LogicException which is not caught, so triggers a E_ERROR
但是,您可能已经知道了,但只是为了确保:您无法以任何方式从 E_ERROR 中恢复.
However, you probably know it already, but just to make sure: you can't recover from a E_ERROR in any way.
至于回溯,你不能...... :(在大多数致命错误的情况下,尤其是未定义函数错误,你并不真正需要它.精确定位文件/行它发生的地方就足够了.在这种情况下,回溯无关紧要.
As for the backtrace, you can't... :( In most cases of a fatal error, especially Undefined function errors, you don't really need it. Pinpointing the file/line where it occured is enough. The backtrace is irrelevant in that case.
这篇关于使用 register_shutdown_function() 处理 PHP 中的致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 register_shutdown_function() 处理 PHP 中的致命错误
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
