确保 PHP 关闭函数被调用

发布于 2024-11-06 06:46:28 字数 1039 浏览 1 评论 0原文

我试图在 PHP 中捕获尽可能多的错误,并以非默认方式正确处理它们。我的问题在这个例子中得到了最好的说明:

<?php 

function errorHandler($errno, $errstr, $errfile, $errline){
    echo "Error handler here <br>\n";
    //throw new Exception($errstr);
}
function shutdownFunction() {
    echo "Hi I'm in here <br>\n";
}

set_error_handler("errorHandler");
register_shutdown_function("shutdownFunction");

try {
    $undefined->ok(); // causes some error
} catch(Exception $e) {
    echo "Caught the exception <br>\n";
}

作为 PHP 程序运行此代码的结果将表明 errorHandler() 已运行,会打印 PHP 错误(如果“display_errors”设置为“On "),然后运行 ​​shutdownFunction()

如果我取消注释该异常抛出,就会出现我遇到的问题;我想尽可能频繁地抛出 PHP 错误异常。如果我取消注释 throw 语句,则会调用错误处理程序,该处理程序会引发异常,从而导致进一步的错误,从而导致 shutdownFunction() 不被调用。

据我了解,我不能将此错误变成可捕获的异常;但是,我至少想确保调用关闭函数时不会限制我捕获至少一些 php 错误作为异常的能力(这意味着我不想删除该 throw< errorHandler() 中的 /code> 语句)。

我可以在 errorHandler 中进行一些检查来检查抛出它是否会导致 shutdownFunction() 被绕过吗?

I'm trying to catch as many errors as possible in PHP and properly handle them in a non-default way. My question is best illustrated in this example:

<?php 

function errorHandler($errno, $errstr, $errfile, $errline){
    echo "Error handler here <br>\n";
    //throw new Exception($errstr);
}
function shutdownFunction() {
    echo "Hi I'm in here <br>\n";
}

set_error_handler("errorHandler");
register_shutdown_function("shutdownFunction");

try {
    $undefined->ok(); // causes some error
} catch(Exception $e) {
    echo "Caught the exception <br>\n";
}

The result of running this code as a PHP program will indicate that errorHandler() is run, a PHP error is printed (if "display_errors" is set to "On"), and then shutdownFunction() is run.

The problem I'm having arises if I uncomment out that exception throw; I want to throw exceptions on PHP errors as often as possible. If I uncomment the throw statement out, then the error handler is called, which throws an exception thus causing a further error which results in shutdownFunction() not to be called.

It is my understanding that I can't make this error into a catchable exception; however, I would at least like to ensure that the shutdown function is called without restricting my ability to catch at least some php errors as exceptions (meaning I don't want to remove that throw statement in errorHandler()).

Is there some check in the errorHandler that I can do to check whether or not throwing it will cause shutdownFunction() to be bypassed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-11-13 06:46:28

从错误处理程序中抛出异常,然后使用 set_exception_handler 处理未捕获的异常。

Throw an exception from the error handler, then use set_exception_handler to handle uncaught exceptions.

羞稚 2024-11-13 06:46:28

所以我解决这个问题的方法是检查 $errstr 参数是否以“未定义变量”开头 - 如果是,那么我不会抛出异常。我想如果还有任何其他错误有类似的问题,我也会对它们做同样的事情。我找不到 php 错误的完整列表及其影响,所以它必须是临时的

So the way I'm solving this is to check if the $errstr parameter starts with "Undefined variable" - and if it does, then I'm not throwing the exception. I suppose if there are any other errors that have a similar issue, i'll do the same thing with those. I can't find a comprehensive list of php errors and what their affects are though, so it'll have to be ad hoc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文