Symfony 6 Certification New exam with updated questions 100% online Show your expertise

New in Symfony 2.8: Polyfill Components

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Contributed by
Nicolas Grekas
in #16240 and #16317.

In web development, a polyfill is "additional code which provides facilities that are not built into a web browser". They are wildly popular in JavaScript applications because they provide a uniform API regardless of the features provided by users' browsers.

This was the very same issue that Symfony was increasingly facing as more diverse PHP versions were being supported, from the ancient PHP 5.3.3 (released on July 2010!) to the cutting-edge PHP 7.0. The differences in those PHP versions made us include lots of PHP version comparisons:

if (PHP_VERSION_ID >= 50400) {
} else {

We also needed to perform a lot of function checks because some important PHP extensions may be missing when executing Symfony applications:

if (function_exists('mb_strlen')) {
    $params[$index] = mb_substr(...);
} else {
    $params[$index] = substr(...);

// ...

Introducing PHP Polyfills

Supporting five different PHP branches across the entire Symfony codebase was becoming a maintenance hell. For that reason, we decided to create several small polyfill components to "fill the gaps" of PHP 5.x versions and missing extensions.

For example, to use some of the great new PHP functions provided by PHP 7.0 in any PHP 5.x application, you just need to install the symfony/polyfill-php70 component:

$ composer require symfony/polyfill-php70

Now your project can start using these functions included natively in PHP 7 and provided via PHP 5.x code by this component:

  • intdiv()
  • preg_replace_callback_array()
  • error_clear_last()
  • random_bytes() and random_int()

This PHP 7 polyfill also provides the *Error throwable classes.

Pick your PHP polyfill

Symfony project has already published 12 decoupled polyfill components, which can be used separately in your applications:

As you can see in this pull request, using all these polyfills allowed Symfony 2.8 to remove around 800 lines of code.

The next PHP polyfills

If you think that some other polyfill might be useful for Symfony or PHP in general, open an issue in the main symfony/polyfill repository and ask for it. You can also propose your own polyfill code, as long as it meets the Symfony quality standard and provides behavior parity with the native version.

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.


This is probably the best feature for 2.8! No more worries which php version your function may or may not support when contributing.
I love this as well, great pick up!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.