Preparing your Applications for PHP 7 with Symfony Polyfills

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:

$ 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.


This is what allowed me to upgrade my project from PHP5.6 to PHP7.1 and finally deploy it on Heroku, I can do nothing else than approve these polyfills 👍👍
How can polyfills help preparing apps to run higher versions of PHP? usually problem of migrating is with BC breaks or deprecations and not with lack of new functions. Using polyfill does not guarantee that you'll upgrade seamlessly, you need to deal deprecations anyway in the same manner if you did not use polyfill

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 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)
@Pavel two comments:

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.
@javier given you may want to fix the "Symfony 4 will need PHP 7.0 or 7.1" part or make it more conditional ;)

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)
@Pavel yes, there are new language constructs that cannot be implemented in these polyfills. But until now you had to choose: stay with 5.3 and use no modern PHP feature or upgrade to 7.x and use modern feature. Polyfills bring another possibility: stay with 5.3, but start using some 7.x features to modernize the app while we wait until we can fully upgrade it to 7.x.
@Niels I've updated the post to mention that Symfony 4 will require PHP 7.1 too. Thanks!
@Pavel polyfill are about outsourcing your technical debts: take e.g. password_hash: before the function was introduced, we had a similar implem in Symfony core. By using a polyfilled implem, we didn't have to wait to raise our lowest req to remove some code, thus reduce the maintenance burden on us.
Does this mean that Symfony 3.4 will also require PHP 7.1? Or are there 2 various branch already?
@Nicolas how polyfilled implementation absense prevents you from bumping lower reqs? Keeping lower deps is about wider BC support not about upgrade in my mind. If my app requires PHP ^5.4 | ^7.0, the drop of lower php versions is matter of updating the composer.json. but if your app is requiring PHP ^5.4 only, polyfill does not help you to make it work with ^7.0. sensio deprecation detector does. phan does. plenty of other tools do. but not the polyfill.

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.
@Pavel yes, we have different perceptions about this. The summary of my position is:

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.
Login with SensioLabsConnect to post a comment