PHP 7 compatibility through Symfony Polyfill, not transpiling

In the JavaScript realm it's become a common practise to transpile code. This means that you write your code in the latest & greatest syntax and translate that to something that is widely supported. That's how JS developers are deploying ES 2015 to production today, even though complete browser or server implementations don't exist.

Update: Transphpile, A PHP 7 to PHP 5.6 transpiler has been made since the writing of the article :)

A transpiler is a source-to-source compiler and transpiling is an additional step in your development workflow. Build tools are already a part of the routine for JavaScript development, so this does not add to daily work. There are even tools that do transpiling transparent to the developer by transpiling at request time, complete with debugging capabilities through Source Maps.

Transpiling is not limited to transpiling from one language version to another. For JavaScript runtimes there are completely separate languages like Coffeescript and TypeScript that are written in a completely different syntax and add functionality like type checking of JavaScript at transpilation time. Gopher.js is another eccentric option.

This lead to some confusion and fragmentation, but once the ECMAScript standard development got back on track after stagnating from 1999 to 2009 developers have been increasingly moving to development with vanilla JavaScript instead of the exotic flavours. This means that the raw source will be native standard code in the future as ES2015 implementations become widely available.

PHP code can be executed in exotic ways (with JPHP, HippyVM or Quercus), but it never had a transpiling compiler before the Hack transpiler. The Hack language from Facebook runs on the PHP compatible HHVM runtime is a completely separate language.

Polyfilling PHP

Continuing with the theme of JavaScript - there are also polyfills for browser features. Modernizr added support for many HTML5 elements and Polymer offers a polyfill for web components. Polyfills (or shims) add support for missing features in the runtime by adding code.

Transpiling and polyfills are complementary techniques, not outright alternatives. Both have their limitations and certainly performance loss, but they're still a pragmatic method for moving to the latest version of the available language.

The 3.0 incarnation of the Symfony Framework bumps up the PHP requirement to 5.5. This is likely the motivation behind the creation of a polyfill now available in the form of the Symfony Polyfill component:

This project backports features found in latest PHP versions. It also provides compatibility PHP implementations for some extensions and functions. You should use it when portability across PHP versions and extensions is desired.

Adding the polyfill allows adopting certain features provided by PHP extensions (written in C) and PHP 7 language features to environments that lack native support for them. The effort is still quite new, but it enables developers to use PHP 7.0 features like random_bytes and random_int functions with PHP 5 compatible runtimes (such as PHP 5, HHVM and JPHP).

PHP has always had a simple development routine: Edit code, save and reload browser. No compilation or other steps have not been required. It is integral that this strenght is kept and by nature the polyfill model is a better way of achieving this. Polyfills are more limited as language features like the spaceship operator can not be added using this method.

By studying the composer.json of the component you can see it's also reusing previously made polyfills instead of writing it's own. Learn more about the Symfony Polyfill component in the PR: A new component for portability across PHP versions and extensions

Not everyone (can) run bleeding edge

In the past shared hosting was popular and the move from PHP4 to PHP5 was not straightforward in an environment with both versions in production use. Nowadays VPSes or PaaS with latest versions are the norm for many, but there are still plenty of production servers running older PHP versions for various reasons.

PHP 5.5 is the current oldest supported PHP version, but PHP 5.3 on RHEL/CentOS 6 will receive Security Fixes from RedHat/CentOS until November 30, 2020. For these cases the Symfony Polyfill component can be a godsend. So the next time you're writing code for a limited hosting environment, take a look at what it has to offer.

HHVM from Facebook Open Source has grown into a credible PHP environment. Wikipedia, Etsy and others use HHVM in production. PHP 7 is a new version of the language that introduces a new PHP engine and improvements to the language itself.

The HHVM project plans to keep up feature parity with the main PHP implementation, so we should get PHP 7 features in the future. But with PHP 7 release imminent, using polyfills together with HHVM seems like an ideal solution, especially since the Symfony project itself is 100% HHVM compatible.

The introduction of the Polyfill component from Symfony is a great opening. A transpiler that does not break the development flow would be welcome as well.

Written by Jani Tarvainen on Saturday October 31, 2015

Permalink - Tags: hhvm, php7, php, transpiling