Facebook’s Hack, HHVM, and the future of PHP

What is Hack and what does it mean for the future of PHP?

Photo: thebusybrain https://www.flickr.com/photos/thebusybrain/3283201861/
Facebook recently released Hack, a new programming language that looks and acts like PHP. Underneath the hood, however, are a ton of features like static typing, generics, native collections, and many more features for which PHP developers have long been asking. Syntax aside, Hack is not PHP. Hack runs only on Facebook’s HipHop virtual machine (HHVM), a competitor to the traditional PHP Zend Engine.

Why did Facebook build Hack?

Much of Facebook’s internal code is first written with PHP. Facebook can onboard new developers quickly with PHP because the language is notoriously easy to learn and use. Granted, much of Facebook’s PHP code is likely converted to a C derivative before being pushed into production. The point is Facebook depends strongly on the PHP language to attract new talent and increase developer efficiency.

Strict Typing

Unfortunately, PHP may not perform as well as possible at Facebook’s scale. PHP is a loosely typed language and type-related errors may not be recognized until runtime. This means Facebook must write more tests early to enforce type checking, or spend more time refactoring runtime errors after launch. To solve this problem, Facebook added strict typing and runtime-enforcement of return types to Hack. Strict typing nullifies the need for a lot of type-related unit tests and encourages developers to catch type-related errors sooner in the development process.

Instantaneous Type Checking

To make the development process and error-catching process even easier, Facebook includes a type-checking server with its HHVM engine. This server runs locally and monitors Hack code as it is written. Developers’ code editors and IDEs can use this type-checking server to immediately report syntax or type-related errors during code development.

What about existing PHP code?

Most existing PHP code will run on Facebook’s HHVM engine. This is necessary because Facebook’s existing codebase is largely existing PHP code. There are a few exceptions since Hack does not implement everything from its traditional PHP equivalent; there are a few language features (like variable variables) and Zend extensions that remain unsupported. Rest assured, the fine folks behind HHVM are doing their very best to achieve optimal parity with the Zend Engine as quickly as possible.

Also, Facebook created the Hack language with a gradual migration from traditional PHP code in mind. To convert a PHP class to a Hack class, change the leading <?php to <?hh. That’s literally all that is necessary. Hack’s additional features are icing on the cake.

Does this mean PHP is dead?

If Hack is so good, does this mean PHP’s days are numbered? Yes and no. The traditional Zend flavor of PHP is managed by a PHP “internals” committee. New features are proposed and voted on by committee members. Due to the inherent nature of committees, progress is slower than some may prefer. The PHP internals committee is also publicly perceived for in-fighting and inexplicable voting results. This doesn’t encourage public favor. On the bright side, PHP development is 100% public and open to feedback, peer review, and suggestions for new features.

But Hack is good. REALLY good. Even I have been tempted to start relying on Hack and HHVM for my daily PHP work. Hack has so many useful features that may never make it into traditional PHP. Facebook’s HHVM engine is more performant than the Zend engine, and it has more internal developers, too, dedicated to constantly making the HHVM engine better.

This does not mean that the PHP Zend engine will disappear. Both the Zend and HHVM engines can and will co-exist (just look at the plethora of Ruby engines). Competition is a good thing, and I hope that both Zend and HHVM will benefit from each other’s strengths.

It is obvious that Facebook cares very much about Hack and HHVM, if only for its own benefit. But we PHP developers can only reap the benefits of Facebook’s hard work now that Hack and HHVM have been released and open-sourced. Why would I not switch to Hack and HHVM full-time?

Is Facebook in charge?

Hack is Facebook’s baby. It is written largely by Facebook engineers, although I believe it was recently open sourced for public feedback. However, Hack and HHVM are still very much dependent on Facebook’s continued participation and investment. If Facebook were to get hit by a bus or simply stop caring, Hack and HHVM would be in trouble. This is exactly why I am hesitant to invest too much time learning Hack or porting existing code to Hack.

Fortunately, there are those inside of Facebook that recognize this very important issue and are pushing for an open governance of Hack and HHVM so the technologies are not tightly coupled to Facebook’s future.

This is a fine line to walk. This morning I was discussing this subject with fellow PHP developers on Twitter. Some were concerned that a third-party government of Hack and HHVM would slow development progress (as most committees do). Hack and HHVM are currently surging ahead at a break-neck pace with a ton of new features and improvements that many PHP developers could only dream of until now. Some careful jurisprudence is in order before making any rash decisions about how, where, and who governs Hack and HHVM.

The Future

Hack and HHVM (in some form) are the future of PHP. The current hesitations hovering over Hack and HHVM will solve themselves as, I believe, Facebook moves Hack and HHVM to a better, longer-term open-government solution. Should you start learning Hack and HHVM? Yes.

Editor’s Note: A previous version of this article incorrectly stated that HHVM is not compatible with “variables variables.” This has been updated in the post to reflect that the feature is only unsupported running Hack code.

Josh Lockhart is the creator of the Slim Framework for PHP. He also created and maintains PHP The Right Way, a popular initiative in the PHP community to encourage best practices and provide quality information. Josh is a senior developer at New Media Campaigns, a full service web design, development, and marketing agency in Carrboro, North Carolina.

tags: , ,