Is it possible to check PHP file syntax from PHP?
Asked Answered
E

5

12

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?

Eppes answered 30/7, 2009 at 17:38 Comment(2)
Do you really consider checking the syntax of every file you want to include every time a script is executed?Sinusoid
Not exactly. I have special kind of classes that could be potentially corrupted, not all of them.Eppes
E
4

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

Electromyography answered 30/7, 2009 at 18:1 Comment(3)
Thanks for your answer. Answer "no, not with a reasonable performance" is a valid option. SVN pre-commit hook is a great idea and I think will solve our problem in most of the cases.Eppes
You're welcome :-) OK about svn pre-commit hook : when you are admin of the SVN server, hooks are really great to use !Electromyography
There is some valid use cases like a devtool CLI watcher to generate cache filesHendon
O
4

Another way you can make one php file in your root directory called checkSyntax.php

<?php
for($i=1; $i < count($argv); $i++){
        $temp = "php -l " . $argv[$i];
        $output = exec($temp);
        echo "\n$output";
}
?>

now, open your bashrc file to make a shortcut to run this file. add below line to run checkSyntax.php

alias checkSyntaxErrors='php /root/checkSyntax.php'

and now goto your source directory do svn st.

it shows you list of files, now easily run the command.

checkSyntaxErrors file1.php file2.php .......

this will check all your files passing as arguments.

enjoy :)

Olshausen answered 26/4, 2012 at 7:6 Comment(0)
H
1

This is an old question, but it seems in recent php versions we can do this

try {
  include_once($file);
} catch (\ParseError $e) {
  // Parse error
} catch (\Throwable $e) {
  // Any other error
}
Hendon answered 11/3, 2022 at 11:56 Comment(0)
P
0

In short: i can't see a way to do this, but have an idea which might be sufficient.

There are log monitoring programs or can filter the logs via standard tools for files with parse errors. If a file appears, you put the villain filename into a black list and your autoloader checks before load against this list.

With this method, at first time you'll serve a blank screen (assumig error reporting to the output are turned on on production servers) but the second will have a page without the faulty component.

In the autoloader you should have a list or naming scheme to always try to loading mandatory classes (other ways your application might be in an inconsistent state)

Putrefaction answered 30/7, 2009 at 17:57 Comment(2)
I assumed he(his system) has no control over those files at build time.Wilcher
Interesting idea, while I think that it is not good to make syntax checking dependant on user visiting the site. Even if only first one will get the blank page.Eppes
K
0

You could also do some unit testing, where you load the PHP you're dynamically executing and assert that exec("php -l $fileName") is valid. If you did that you'd be able to verify it once in your tests, generating it with appropriate variables, and have a reasonable level of confidence your PHP was good.

Kilo answered 10/2, 2014 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.