PHP version compatibility check

后端 未结 3 1116
北恋
北恋 2021-02-06 05:33

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? M

相关标签:
3条回答
  • 2021-02-06 05:57

    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.

    0 讨论(0)
  • 2021-02-06 06:00

    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.

    0 讨论(0)
  • 2021-02-06 06:07

    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.

    0 讨论(0)
提交回复
热议问题