Getting Started With PHP Upgrades

Do you have an application running on an outdated version of PHP? Did your attempt to upgrade show a lot of errors, and now you don't know where to start? With this short guide, you'll be able to get started with PHP upgrades and stay on the right track.

There are three major steps to an upgrade. Finding the compatibility problems, fixing them, and then making sure that everything keeps working.

Compatibility Problems

There is an amazing tool called PHPCompatibility. It's a composer package that you install on your development machine. You tell it to which version you want to upgrade, it will scan your code, and display a report of all the incompatibilities.

Although useful, it can't find everything, because some incompatibilities depend on the larger context of your application. PHPCompatibility prefers to not flag false positives. You may uncover additional issues during testing, but at least you would have eliminated many issues upfront.

If you already have Docker installed on your machine, you can run this tool more easily with phpco.

Fixing Problems

I fix many of my issues using 3v4l (read as eval). I can extract an isolated example of the faulty code to this tool, solve it in the most generic way, then execute using multiple PHP versions to see whether it works. Try the following:

$response = '1234567890';
$header = 'Content-length: ' . strlen($response) + 1;
echo $header;
$response = '1234567890';
$header = 'Content-length: ' . (strlen($response) + 1);
echo $header;

This tool is also helpful if you want a solution that is compatible with multiple PHP versions, and can even plan ahead for not yet released versions.

Testing Solutions

If you have no automated tests, testing will be the most time-consuming step, and will need to be repeated for every upgrade. I strongly recommend that you at least start to write a few tests as part of this upgrade, so that you'd have that much less to retest next time.

I like to go for acceptance tests first. They don't require any code refactoring, and ensure that the behavior, as seen by the application's user, does not change. I usually go with Behat + Guzzle, where I send requests to the application and assert the responses.

For any manual tests, it's important to have two test servers: one running the original code on the old PHP version, and the other running the latest upgraded code on the new PHP version. We want to distinguish which issues are new, and which issues always existed. Perhaps we're only noticing these issues now because we're doing more thorough testing than usual.

This exercise will possibly uncover new compatibility issues which you'll need to fix and retest.

I hope this helps you with your upgrade. If you need extra help, I offer upgrades as a service. Drop me a line!

Learn about more topics