使用包含时捕获 PHP 解析器错误 [英] Catching PHP Parser error when using include

查看:21
本文介绍了使用包含时捕获 PHP 解析器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 functions.php 的文件.

I have a file called functions.php.

该文件包含对所有其他函数文件的包含,例如:

This file consists includes to all the other function files, for example:

include_once("user_functions.php");
include_once("foo_functions.php");

我想捕获错误,当我在其中一个文件中添加代码时,它不会将错误传递给整个系统.

I would like to catch errors where when I screw a code in one of those files, It wouldn't give the error to the entire system.

例如,如果 foo_functions.php 中存在解析器错误,则不会将其包含在 functions.php 中.

For example, if there is a parser error in foo_functions.php it will just not include it in functions.php.

这可能吗?

推荐答案

解析器错误是致命错误,因此您无法捕获它们.查看此问题和答案以了解更多详情.

Parser errors are fatal errors, so you can't catch them. See this question and answer for more details.

如果您可以运行 exec(),您可以做的是调用 php -l thefilename.php 并检查结果.有关其工作原理的信息,请参阅手册.但是,这里存在一些问题:

What you can do if you can run exec() is call php -l thefilename.php and check the result. See the manual for information on how this works. There are a few problems here, however:

  • 极其危险,因为您正在向命令行传递信息.您绝对必须非常仔细地过滤任何用户输入,否则您将允许用户非常广泛地访问您的系统.
  • exec() 经常被禁用,因为不正确使用它会带来极高的安全风险.
  • 确实没有充分的理由包含您尚未验证语法错误的文件.如果这是用于插件或其他东西,那么我理解您的推理.但是,如果是您可以控制的代码,则应在将其投入生产之前进行验证.
  • This is extremely dangerous, because you are passing information to the command line. You absolutely must filter any user input very carefully, or you would be giving the user very broad access to your system.
  • exec() is often disabled, as it should be, because of the extremely high security risks of using it incorrectly.
  • There's really no good reason to include a file that you haven't already validated for syntax errors. If this is for plugins or something, then I understand your reasoning. If it is code you have control over, however, you should validate before putting it into production.

这篇关于使用包含时捕获 PHP 解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆