是否可以从PHP检查PHP文件语法? [英] Is it possible to check PHP file syntax from PHP?

查看:84
本文介绍了是否可以从PHP检查PHP文件语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过自动加载功能动态加载PHP类文件. 这些文件可能由于某种原因而丢失或损坏.

I load dynamically PHP class files with autoload. And those files could be missing or corrupted by some reason.

自动加载将成功报告丢失的文件,以便应用程序逻辑可以处理.但是,如果这些文件已损坏,则整个过程将停止,并为用户显示空白屏幕,并在错误日志中显示"PHP Parse错误:语法错误".

Autoload will successfully report missing files so application logic could handle that. But if those files are corrupted, then the whole processing halts with blank screen for the user and "PHP Parse error: syntax error" in error log.

是否可以从PHP代码检查PHP文件的语法?

Is it possible to check syntax of PHP file from PHP code?

我看过这里: http://us. php.net/manual/zh-CN/function.php-check-syntax.php -已过时.

I've looked here: http://us.php.net/manual/en/function.php-check-syntax.php - it's deprecated.

还有

exec("php -l $file");

似乎是错误的方式( http://bugs.php.net/bug .php?id = 46339 )

有想法吗?

推荐答案

您真的不应该在执行时尝试检查不正确的PHP文件:这会浪费应用程序的响应时间!

You really shouldn't try to check for non-correct PHP files at execution time : it'll kill the response time of your application !

更好的方法"是在修改完PHP脚本后从命令行使用php -l;或如果使用的话,将它包含在您的构建过程中;或将其作为SVN预提交挂钩插入(如果您使用的是SVN,并且可以定义 SVN钩子.

A "better way" would be to use php -l from command line when you're done modifying a PHP script ; or include it in your build process if you're using one ; or plug it as an SVN pre-commit hook if you're using SVN and can define SVN hooks.

我认为,几乎所有给定的解决方案都比在执行时检查自己好!

In my opinion, almost any given solution would be better than checking that yourself at execution time !


考虑到像您要避免的错误这样的错误可能不会经常发生,最好是...让它们发生.
唯一的事情是:激活日志并对其进行监视,以便在出现问题时能够快速检测到:-)


Considering errors like the ones you want to avoid will probably won't happen often, it is probably better to... just let them happen.
ONly thing is : activate logs, and monitor them, the be able to detect quickly when tere is a problem :-)


当然,这不会阻止您处理丢失文件的情况;但这是另一回事...


Of course, this doesn't prevent you from dealing with the case of missing files ; but that's a different matter...

这篇关于是否可以从PHP检查PHP文件语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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