Alternative PHP Implementations

Note: Our good friends at the Development Hell podcast wrote this great guest piece for us about different kinds of PHP implementations. Thanks guys!

Many PHP developers have a very vague understanding of what is meant by a "runtime". It's a concept that is more common in other languages languages. At it's most basic level, it is the idea that you can have a specification for the behaviour of the language, and then you can write your own implementation of it.

Why would someone want an alternative implementation? There are many reasons for doing it.

A shocking fact: programming languages have bugs and are sometimes missing features that people desire. Alternative implementations are a great way for people to try and create a version of a language that meets their needs while hopefully remaining as backwards compatible as possible.

For those familiar with Ruby, Ruby MRI (Matz's Ruby Interpreter) is considered the baseline implementation of the language. In an attempt to gain better performance or allow the language to be run in different environments, people have written alternative implementations of Ruby. Rubinius, JRuby and Ruby Enterprise Edition are all attempts to provide alternative implementations for different types of Ruby users.

Python also has an alternate implementation that is becoming more widely used. PyPy started out as an implementation of Python in Python itself, but has changed to become a tool for actually writing other languages through the use of RPython (Restricted Python), along with supporting micro-threads for concurrency. RPython could cover an entire blogpost so I highly recommend checking it out.

HHVM

HipHop Virtual Machine for PHP (more commonly known as HHVM) is an open source project that is the result of Facebook's efforts to make PHP more performant. Initially starting out as a tool that compiled PHP into C++ binaries, it is now a virtual machine with a JIT (Just In Time) compile used by Facebook to run all it's PHP code.

Why would Facebook do this? Two main reasons.

The first reason is that Facebook was looking for ways to get more performance out of PHP. Less resources consumed means less servers Facebook needs. Given the ridiculously large number of servers they run, we are talking about serious non-Bitcoin currency being saved. Initial efforts yielded a 50% savings, which is impressive. But it was limited to just PHP 5.2. Facebook needed more.

Next, an alternative implementation also gave Facebook the opportunity to add new features to the language that would benefit them while also fixing bugs in the language itself. Facebook does have some employees who are also part of the PHP core team, so they would have the necessary skills to understand some of the problems and the potential solutions.

In addition to trying to create an implementation of PHP that will run faster than the C version, Facebook has been hard at work adding things like gradual typing via the difficult-to-search-for-and-they-are-sorry Hack and an interactive debugger.

HHVM is currently working well with PHP 5.4 but the project is working hard to stay as close as possible to the latest stable version of PHP (currently PHP 5.5) and continue to work on making HHVM run as fast as possible.

Links to check out: HHVM PyPy

About PJ Hagerty

Developer, writer, speaker, musician, and Team Lead of an elite band known as the Engine Yard Community Team, PJ is known to travel the world speaking about programming and the way people think and interact. He is also known for wearing hats.