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.
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.
Videos
What can the WP Engine PHP Compatibility Checker be used for?
Can the plugin detect runtime compatibility issues?
Why use Coding Styles checker is needed?
Hello,
I have legacy-ish codebase (largely updated and working with PHP 7.4) and I would like to check it for compatibility with PHP 8, however I've yet to find a reliable tool for the job. My current search has taken me to:
- phpCompatibility (https://github.com/PHPCompatibility/PHPCompatibility) works with PHPCs, but seems that targeting PHP8 is a mixed bag (judging by issues that mention some check as WIP)
- Phan (https://github.com/phan/phan) seems that it can check for backward compatibility but I've yet to find a way to configure it for forward-compatibility
Next steps:
- Rector
Has anyone used these or something else to prepare for upgrades to PHP 8 as of yet? I'd appreciate any hints.
Thanks!
Do what our cto did, just put v8 on the server and composer, push and see what smells bad.
What a fun week that was.
I very recently migrated a decent-sized legacy project from 7.2 to 8.0 using Rector and can definitely recommend it. The best part about Rector is that it can be used to either partially or fully automate the migration, as well as handle a lot of other things besides the version compatibility (e.g. identifying dead code blocks).