简体   繁体   中英

PHP version compatibility check

How do I know my code which version of the language is compatible? Is there a way to figure out which is the minimum version of PHP language that can execute it without error? Maybe there is some Checker or a function, method, class?

Most likely you need to have different PHP versions installed. Then you can check compatibility of your code with specified PHP version using shell:

find . -name *.php | xargs -n1 /usr/bin/php -l

php -l command runs PHP in syntax check only mode. The command above will check each PHP file in your project againts compatibility with PHP version located at /usr/bin/php.

Install the PHP version you want to test and run php -l file.php to test if the file passes the lint check. If PHP is unable to parse the file, it will tell you.

In general, you should be aware of which features became available in which version. Disregarding PHP 5.3 with its several patch versions that added significant improves for a moment, this boils down to knowing what features were brought with PHP 5.4, 5.5, 5.6 and 7.0, and explicitly pointing to that version in your composer.json .

As a hint: 5.4 has short array syntax and traits, 5.5 has generators and finally , 5.6 comes with variadic functions and argument unpacking, and 7.0 has scalar type hinting and return types. It helps to use a IDE that knows about these features and warns you if you use something that isn't supported in the version you selected.

PHP comes with a constant PHP_VERSION that contains the current version you are running on, and has a function version_compare() to allow for easy comparing of version notation as in "which one is greater". This would allow to execute different parts of the code depending on the version, allowing to add compatibility layers for some things you need if you run on lower versions, and using the PHP implementation (usually faster) when running on more recent versions.

Apart from this, you will always stumble upon problems with extensions not being installed. This isn't a problem with the PHP version itself. PHP has function_exists() and method_exists() to detect if you can call something before you do (and fail with a fatal error). You can detect this error condition and either have a different solution, or inform the user he has to add something to his PHP installation.

I would recommend using Travis CI for open source projects. Basically you get it for free, and adding different PHP versions is like adding a new line in the travis.yml configuration file. They do offer plans for private repositories as well. Any other CI installation offering you plenty of PHP versions will also work as long as you are running your code on all the PHP versions you intend to support.

Final suggestion: Drop support for PHP 5.3 and 5.4. These versions are out of maintenance (or leaving security-only fix phase in 2 months from now) and shouldn't really be targeted anymore.

https://3v4l.org/

This online tool shows your code output for more than 150 different PHP versions (every version released since 4.3.0) plus HHVM.

Not sure if it's enough for your purposes.

The Docker provides the ability to follow a suggested by pamelus proposal with a really small efforts:

docker run --rm -ti -v /path/to/your/app/src:/app php:8.0 bash
find /app -name '*.php' | xargs -n1 /usr/local/bin/php -l > /app/reportphp8

So php:8.0 is a tag which can be obtained from DockerHub page of PHP . By changing this version your can easily check your app's compatibility with 8.1/9.3/whatever version you want.

/path/to/your/app/src is a directory of your application sources root (the directory containing source code of YOUR application only, ie src/ dir in Symfony and app/ in Laravel. That's done to prevent checking syntax of the vendors dir).

The report will be stored in your application root named reportphp8 .

Important

The advice above is about the syntax only, ie it can detect whether you use removed or deprecated language structures or core functions. It won't be able to detect if you supply the correct types to functions having changed signature, etc. For those purposes use static analyzers like Pslam / PHPStan / Phan .

I used the PHPCompatibility/PHPCompatibility library to check my project's version compatibility, I recommend.

You can specify the version of PHP you want to check. For example, to test PHP 7.3:

./vendor/bin/phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 7.3

Here's how to test your code for php 8.0 compatibility (slightly improved onhttps:\/\/odan.github.io\/2020\/12\/22\/php8-compatibility-check.html<\/a> ):

composer require --dev squizlabs/php_codesniffer phpcompatibility/php-compatibility

phpcs --config-set colors 1

vendor/bin/phpcs src -p --report=code --ignore=vendor/* --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM