PHP在Include文件中在致命错误后工作。错误在重新返回中消失了
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHP将大多数错误更改为异常。这是在
这包括在内文件中包含解析器错误,因为
include()
在运行时调用。由于在任何代码运行之前检测到这些错误,因此它不会在主脚本中包含解析器错误,并且如果有任何解析错误,则无法运行代码。出于同样的原因,如果包含文件中的任何地方都存在语法错误,则不会执行任何代码,因为它必须在执行任何文件之前必须分析整个文件。因此,如果存在语法错误,
$ res。='foo是加载。'
将永远不会执行,当您返回$ res
时,您将不会收到该消息。正如上一页上还提到的,
错误
不是异常的子类
(他们不希望所有这些新错误被现有异常处理程序)。因此,您必须用来
捕捉它。
至于为什么您可以在
最后返回
块中,这在PHP changed most errors into exceptions. This is documented in Errors in PHP 7:
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 ofException
(they didn't want all these new errors to be caught by existingException
handlers). So you have to useto catch it.
As for why you can return in the
finally
block, that's explained in the documentation of exceptions: