PHP在Include文件中在致命错误后工作。错误在重新返回中消失了

发布于 2025-02-07 02:04:43 字数 1278 浏览 2 评论 0原文

index.php

<?php

function bar()
{
    try {
        $res = 'loading foo:';
        include 'foo.php';
    } finally {
        return $res . 'finally.';
    }
}
$res = bar() . " Why it's working???";

# php 7.1.33 is work
# php 7.4.21 is work
# php 8.0.8 is work

echo $res; // "loading foo:foo is load.finally. Why it's working???"

foo.php

<?php

$res .= 'foo is load.';
$smth = '';
$smth->getBar();

如果在foo.php中是语法错误该样本将无效地错误...但是为什么?

使用此功能是不安全的,您对此有什么了解?

我想在项目中将其用作“轻松捕获”以进行控制需要模块文件:

function bar()
{
    $path = 'module.php';
    $res = '';
    ob_start();
    try {
        include $path;
        $res = ob_get_clean();
    } catch (Exception $e) {
        $res = 'Module has error (' . $path . '): file:' . $e->getFile()
            . ', line:' . $e->getLine()
            . ', mess:' . $e->getMessage();
    } finally {
        ob_clean();
        return $res ?: 'Module has Fatal Error (' . $path . ')';
    }
}

echo bar();

这是致命错误的工作,但是没有回溯:(

我尝试为此功能找到更多信息或示例

index.php

<?php

function bar()
{
    try {
        $res = 'loading foo:';
        include 'foo.php';
    } finally {
        return $res . 'finally.';
    }
}
$res = bar() . " Why it's working???";

# php 7.1.33 is work
# php 7.4.21 is work
# php 8.0.8 is work

echo $res; // "loading foo:foo is load.finally. Why it's working???"

foo.php

<?php

$res .= 'foo is load.';
$smth = '';
$smth->getBar();

If in the foo.php will be syntax error this sample will be work without fatal error... but why?

It's not safe use this feature, what you know about it?

I want to use it in my project as "easy catch" for control require module files:

function bar()
{
    $path = 'module.php';
    $res = '';
    ob_start();
    try {
        include $path;
        $res = ob_get_clean();
    } catch (Exception $e) {
        $res = 'Module has error (' . $path . '): file:' . $e->getFile()
            . ', line:' . $e->getLine()
            . ', mess:' . $e->getMessage();
    } finally {
        ob_clean();
        return $res ?: 'Module has Fatal Error (' . $path . ')';
    }
}

echo bar();

it's work for Fatal Errors, but without backtrace :(

I try to find more information or samples for this feature

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

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

发布评论

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

评论(1

地狱即天堂 2025-02-14 02:04:43

PHP将大多数错误更改为异常。这是在

PHP 7改变了PHP报告的大多数错误。现在,大多数错误现在通过投掷错误例外报告了大多数错误,而不是通过传统的错误报告机制报告错误。

这包括在内文件中包含解析器错误,因为include()在运行时调用。由于在任何代码运行之前检测到这些错误,因此它不会在主脚本中包含解析器错误,并且如果有任何解析错误,则无法运行代码。

出于同样的原因,如果包含文件中的任何地方都存在语法错误,则不会执行任何代码,因为它必须在执行任何文件之前必须分析整个文件。因此,如果存在语法错误,$ res。='foo是加载。'将永远不会执行,当您返回$ res时,您将不会收到该消息。

正如上一页上还提到的,错误不是异常的子类(他们不希望所有这些新错误被现有异常处理程序)。因此,您必须用来

catch (Error $e)`

捕捉它。

至于为什么您可以在最后返回块中,这在

此外,如果最后块还包含返回语句,则返回> block的值。

PHP changed most errors into exceptions. This is documented in Errors in PHP 7:

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

This includes parser errors in include files, since include() is called at runtime. It doesn't include parser errors in the main script, since those are detected before any code runs, and it can't run the code if there are any parse errors.

And for the same reason, if there's a syntax error anywhere in the include file, none of the code there will be executed, because it has to parse the entire file before executing any of it. So if there's a syntax error, $res .= 'foo is load.' will never be executed and you won't get that message when you return $res.

As also mentioned on the above page, Error is not a subclass of Exception (they didn't want all these new errors to be caught by existing Exception handlers). So you have to use

catch (Error $e)`

to catch it.

As for why you can return in the finally block, that's explained in the documentation of exceptions:

Additionally, if the finally block also contains a return statement, the value from the finally block is returned.

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