
It’s quite common in having the web development environment with one version of PHP and then using another version in the production environment. This can cause quite a hassle and make it difficult to manage. Therefore, a developer needs to be able to set restrictions to not just for the dependencies, but also the platform itself, PHP. Luckily there is a solution for those using Composer to manage PHP dependency packages.
PHP Platform
By setting the PHP platform version in the “composer.json” file, this will restrict Composer packages and extensions being installed to only install versions that are compatible to the fake PHP platform version, not the actually PHP version. So next time an installation or update is done with Composer the PHP platform version will be used instead of the actual PHP version installed. This will ensure both development and production environment remain compatible. Though do keep in mind that you may need to require certain package versions to also work within both environments.
The platform setting can be achieved manually by using a text editor or just be simply running a command within your project directory.
$ composer config platform.php 8.0.0
{
"config": {
"platform": {
"php": "8.0.0"
}
}
}
For global system setting add the option, “-g” as shown below.
$ composer config -g platform.php 8.0.0
{
"config": {
"platform": {
"php": "8.0.0"
}
}
}
Ignoring Platform Requirement
If for some reason you need to ignore the PHP platform setting, you can run the following command to override. Just replace the package name “symfony/mailer” in the example below to any package you desired.
$ composer update symfony/mailer --ignore-platform-reqs
$ composer install --ignore-platform-reqs
Check Platform Requirements
It is recommended to verify platform requirements by running the following command as a part of the deployment process.
$ composer check-platform-reqs
Checking platform requirements for packages in the vendor dir
ext-gd 8.1.13 success
ext-mbstring * success provided by symfony/polyfill-mbstring
ext-pcre 8.1.13 success
php 8.1.13 success
Find PHP Version
$ php -v
PHP 8.1.13 (cli) (built: Nov 23 2022 16:30:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
$ php7 -v
PHP 7.4.33 (cli) (built: Nov 7 2022 21:50:25) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
This is post 45 of 100, and is round 2 of the 100 Days To Offload challenge.
References
- Baton (conducting), Wikipedia
- Composer logo, Wikimedia Commons
- Composer
- Conducting, Wikipedia
- Config Platform - Composer
- PHP: Hypertext Preprocessor
Changelog
-
- change topic
-
- change 100DaysToOffload message