Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check PHP file syntax from PHP?

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

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.

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

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

And

exec("php -l $file");

seems to be a wrong way (http://bugs.php.net/bug.php?id=46339)

Thoughts?

like image 657
Oleksandr Yanovets Avatar asked Jul 30 '09 17:07

Oleksandr Yanovets


1 Answers

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

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...

like image 79
Pascal MARTIN Avatar answered Nov 13 '22 19:11

Pascal MARTIN