PHP require 语句 - 失败,但抛出 E_WARNING 而不是 E_COMPILE_ERROR
我将系统中的一个文件移动到一个子文件夹中进行组织,这又破坏了 require
语句的相对路径。这应该会导致 E_COMPILE_ERROR
警告。但是,我们在应用程序中的自定义错误处理程序并没有捕获该错误,而是将错误捕获为 E_WARNING
。因此,我们的客户处理程序记录了该消息,并且生成的 PHP 屏幕显示为空,就像运行了 die();
一样。以下是我们本地处理程序创建的日志:
E_WARNING main(XXXXXX.php): failed to open stream: No such file or directory xxx/xxx.php Line 9 01-26-2012 09:44:27 AM 01 -26-2012 10:03:24 AM
值得注意的是,我们的系统仍在使用可怜的旧 PHP 4
。
知道为什么 require 会抛出 E_WARNING 吗?由于我们看不到任何错误消息,因此花了更长的时间才找出问题所在。
编辑:该应用程序肯定是在执行要求。自定义错误处理程序只是使用 PHP 的 set_error_handler 函数。因此,除了警告、通知和用户错误之外,不应调用它。
附加编辑:
也许结构与此有关?它的工作原理如下: (-> 表示包含,=> 表示需要)
main->File1.php=>File2.php
是否会因为包含 File1.php 但不是必需的而引发警告?这对我来说可能有意义,但似乎是一个小故障?我可能需要在 PHP 5 上对此进行测试。
解决方案
我扫描了我们的应用程序并发现了这个:
error_reporting(E_ERROR | E_WARNING | E_PARSE);
因此,E_COMPILE_ERROR不会出现。此外,@DaveRandom 关于它抛出警告,然后抛出致命错误是正确的。
将其更改为 error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR); 显示错误消息。
I moved a file in our system to a sub folder for organization, which in turn broke a require
statement's relative path. This should have resulted in a E_COMPILE_ERROR
warning. However, instead of that error, our custom error handler in the application caught the error as a E_WARNING
. So, our customer handler logged the message and the resulting PHP Screen came up empty, as if die();
was run. Here is the log created by our local handler:
E_WARNING main(XXXXXX.php): failed to open stream: No such file or directory xxx/xxx.php Line 9 01-26-2012 09:44:27 AM 01-26-2012 10:03:24 AM
It may be important to note that our system is still using poor old PHP 4
.
Any idea why require would throw an E_WARNING? This took a bit longer to figure out what the issue was, since we couldn't see any error message.
Edit: The application was definitely doing a require. The custom error handler is simply using PHP's set_error_handler function. So it shouldn't be called for anything other than warnings, notices, and user errors.
Additional Edit:
Maybe the structure has something to do with this? Here's how its working:
(-> means includes, => means requires)
main->File1.php=>File2.php
Does it throw a warning because File1.php is included, but not required? This might make sense to me, but would appear to be a glitch? I might need to test this on PHP 5.
RESOLUTION
I've scanned through our application and found this:
error_reporting(E_ERROR | E_WARNING | E_PARSE);
Therefore, E_COMPILE_ERROR will not show up. Furthermore, @DaveRandom is correct about it throwing a warning, then throwing a fatal error.
Changing it to
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
shows the error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您对不存在的文件调用 Require 时,它至少会发出此警告一次。然后它会发出一个
E_COMPILE_ERROR
并终止。但是:
因此,恐怕您的自定义错误处理程序永远不会捕获该错误。
Require emits this warning at least once when you call it on a file that does not exist. It then emits an
E_COMPILE_ERROR
and dies.However:
So your custom error handler will never catch the error I'm afraid.
它确实在做要求吗? E_WARNING 似乎暗示您在那里使用 include 而不是 require...或者 PHP4 对此的行为可能有所不同?老实说我不记得了。
Is it indeed doing a require? The E_WARNING seems to imply you use include instead of require there... or perhaps PHP4 behaved differently with regards to this? I honestly can not remember.