Fake PHP Version in Composer

February 23rd, 2019

I always work on many projects simultaneously. Most of them are legacy running on versions as old as PHP 5.3. I don't always set up a separate Vagrant or Docker on my machine, for reasons beyond the scope of this article.

This is about a specific problem that I ran into. Say you run the latest PHP version on your machine, but the server runs PHP 5.5. This means that when you decide to add a Composer package, it might propose a version that may not be installable on the server. The solution is surprisingly easy. You can instruct Composer to see the PHP version of your choosing.

In your composer.json, add the following item in the config section, where the version is the one that runs on your server:

"config": {
    "platform": {
        "php": "5.5.9"

That's it. For example, if you decide to add PHPUnit, instead of offering version 8.0, it will propose 4.8, which is the highest version compatible with PHP 5.5.

Granted, none of these are supported anymore, but sometimes you just need to take things one step at a time before you can upgrade large applications.

Previous: What Do Speakers Look for in Reviews? Next: Randomizing Test Data is a Bad Idea