Jahsiah Deguchi
Jahsiah Deguchi

Reputation:

Does PHP have a built-in means of determining the oldest PHP version compatible with a given PHP script? Or will it ever?

I wish to be able to do something like:

php --determine-oldest-supported-php-version test.php

And get this output:

7.2

That is, the php binary checks test.php for all function calls or syntax used, and then spits out the oldest version of PHP which would be able to run the script.

The purpose would be for me to create a script which goes through all the .php files in my library/framework and figures out which version of PHP I should consider to be the "oldest supported version".

Currently, I'm doing this manually. Whenever I knowingly use a new feature, function or syntax, I bump up a variable to "7.4" or whatever, and then compare the currently used PHP version against that on runtime. This is not ideal, as you can imagine, and I could very well forget to bump up this value or bump it up too much, or too little, etc.

It would be much nicer to be able to determine this automatically, with such a feature in PHP itself.

I have of course looked through the list of options and PHP has no such feature as far as I can tell. Since I basically answered my own question right away, are there any plans on such a feature in the future?

Upvotes: 3

Views: 98

Answers (1)

harrymc
harrymc

Reputation: 1069

The only guarantee given is that PHP will remain compatible within the same major version. You may be interested in looking at the article Why You Should Be Using Supported PHP Versions.

The tool you are looking for is the GitHub project PHP_CodeSniffer, further described in the article PHPCompatibility update.

You may run it using a command such as:

phpcs --standard=PHPCompatibility --runtime-set testVersion 5.4 <path-of-your-php-files>

Upvotes: 6

Related Questions