ACs
ACs

Reputation: 1445

PHP precheck version

I have a framework written in PHP, which examines the current installed PHP version and compares it to the required version, and If the current version is lower than the required gives an error. This is useful because I use functions and syntax which are not available in older versions. For Example this won't work on v5.2.2:

public static function array_flatten(Array $input){
    $return = array();
    array_walk_recursive($input, function($a) use (&$return) { $return[] = $a; });
    return $return;
}// array_flatten

In my framework v5.3.13 PHP is required at least, and Im testing with v5.2.2
My problem is in this case, that the version checking happens before everything other actions. The critical code above is in a separate file which is autoloaded when needed. At the point where I check PHP version, nothing have included that separate file, PHP have nothing to do with it at that time but I got the error:

Parse error: syntax error, unexpected T_FUNCTION in D:\Munka\wamp\www\lmvc_trunk\utils\Utils.php on line 152

instead the outdated version message I intended to print out. (After this error message I call exit())

So the question is: Why is the Utils.php parsed when it is autoloaded and no one invoked any methods of it (So it should not be included thus should not be parsed) before the exit() command?

If I comment out the core of array_flatten method, the error message about the outdated version is displayed properly.

Upvotes: 0

Views: 50

Answers (1)

Erik
Erik

Reputation: 3636

If a file is autoloaded, it has to immediately be parsed. It could contains commands that need to be run. If it cannot be parsed, PHP cannot know what the next step has to be.

When loading a file, PHP cannot know that it only contains function definitions and skip loading those until they are called. It has to parse everything.

Apparently something decided to include the file. Fire up a debugger at the top of the file and see which file is causing it to be loaded.

Upvotes: 1

Related Questions