Preparing your Applications for PHP 7 with Symfony Polyfills
May 17, 2017 • Published by Javier Eguiluz
According to the May 2017 PHP Stats, 53% of PHP developers use PHP 7.0 or 7.1, but only 10% of Composer packages require PHP 7.0 or higher. In fact, 1 in 4 packages still require PHP 5.3, which is used by less than 1% of developers.
Symfony Flex, the new way to create and manage Symfony applications, already requires to have PHP 7.1 installed in your development machines. Symfony 4, to be released in November 2017, will require to have PHP 7.1 in your production servers. Other popular PHP projects have announced similar plans, so if you don't upgrade to PHP 7 soon, you won't be able to install the new versions of the most important PHP projects.
Upgrading your development machines is usually a simple task, but upgrading the rest of the infrastructure (servers, tools, etc.) usually requires more resources. This is where Symfony Polyfills can help you preparing the code of your application for PHP 7.
A polyfill is a PHP 5.3 package that implements functions and features of higher
PHP versions. For example, functions such as boolval()
, array_column()
and password_*()
are only available in PHP 5.5 or higher. However, if you
install the Symfony Polyfill / Php55 package, you can use them in your PHP
5.3 application:
1 2
$ cd your-project/
$ composer require symfony/polyfill-php55
Symfony Polyfills check if a function exists in your PHP version and if it doesn't exist, it registers a new global PHP function with that name. This means that you can use the latest PHP features in your PHP 5.3 applications and when you finally upgrade them to PHP 7, the code will be already using the latest PHP features.
We have created polyfills for PHP 5.4, 5.5, 5.6, 7.0 and 7.1 and you can use them in any PHP application, not only in Symfony applications. In addition, we've created other polyfills for popular PHP extensions, so you can use those features without actually having installed those extensions: APCu, Mbstring, Iconv, Intl ICU, Intl Grapheme, Intl Normalizer, and XML.
In summary, you have just a few months to upgrade your infrastructure to PHP 7, but you can already start using modern PHP features in your PHP 5.3 applications thanks to the Symfony Polyfills.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Polyfills can help you support your apps and libraries for wider range of versions (including older ones), making your life a bit easier.
Also your polyfills has a bit confusing readme. i.e
https://github.com/symfony/polyfill-php54 readme says that
This component provides functions unavailable in releases prior to PHP 5.4:
But really it provides only functions introduces in PHP 5.4 and not above (i.e boolval is introduced in 5.5, which is unavailable in 5.3, which is prior to PHP 5.4, according to article, but this polyfill does not not provide it)
1) Polyfills are not cumulative. 5.5 polyfill only contains 5.5 features, not 5.4 + 5.5.
2) How can polyfills help you? If you are stuck in a PHP 5.3 app because DevOps haven't upgraded the infrastructure yet in production to run PHP 7, you can't use any of the great modern PHP functions. Some day DevOps will upgrade to PHP 7 ... but your app will still be PHP 5.3. However, if you use polyfills, you can start using some PHP 7 features in your PHP 5.3 app. The day DevOps upgrades to PHP 7, your app will be almost PHP 7 too.
1) Yes, I know, but readme states the opposite, as I mentioned
2) If you can overcome lower interpreter version with polyfills - you don't really need to upgrade it. But the most advantages of new versions are not only software (classes, interfaces, functions, etc) but also infrastructure - syntax, performance, etc:
* you will not get the generators with 5.6 polyfill
* you will not get php 7.0 performance
* you will not get strict typehinting and scalar typehints with 7.0 polyfill
* you will not get the ?? operator with 7.1 polyfill
If you are able to run your code on higher versions of PHP - you are already prepared for them and no polyfills needed. If not - polyfill won't help. Everything else is about your logic upgrades and keeping backward compatilbility, if your project is distributable app or library)
Polyfills, as you say, is about using new features in your app on wider platform range, but not about preparing your app for new platforms.
I think we are putting different terms into the word "Application", you and @Javier speaking about fancy DX, easier support etc, but in my mind "Application" is also matter of distribution and deployment, and for me "preparing for PHP 7" is not only using some new features, but also a capability to run with new environment.
If your company/environment is stuck in PHP 5.x, your application doesn't have to be stuck too. Add Symfony Polyfills to your app and start using some PHP 7 features.