An Intro to HHVM and Hack for PHP

Article summary

HHVM and Hack are some interesting innovations that Facebook has brought to the PHP ecosystem. In this post, I’m going to explore a few aspects about each of these technologies, including their features, development experience, and platform support.

Just as a refresher, PHP is a server-side scripting language with a reputation for being notoriously inconsistent and hard to debug (because of features like its failure obliviousness and surprise type conversions). PHP also has a huge historical ecosystem of libraries and frameworks like WordPress.

HHVM

HHVM is a JIT (Just-In Time) compilation VM that runs both PHP and Hack. HHVM evolved out of a static compiler Facebook made for PHP, which is called Hip-Hop. One of the advantages of HHVM is the greatly improved execution time of PHP, about 2x-3x in most circumstances. HHVM also uses less memory to execute each incoming request than PHP.

When it comes to running and developing with HHVM, there are a number of options. There are official Debian and Ubuntu packages, as well as official Docker images. Docker has native support for running HHVM. The OSX support is currently considered experimental. There is currently no support for Windows or 32-bit operating systems.

There is a wide amount of editor support, from Vim to Sublime. HHVM supports most extensions for that work on PHP, such as MongoDB or PostgreSQL interfaces.

Hack

Hack is a partner language to HHVM. It’s very heavily influenced by PHP, but it is an independent language. One of the biggest advantages it offers over PHP is the addition of static typing. This typing is done through annotating the return types of functions and the arguments to functions.

All of the conventional types are present, as well as some extra types such as nullable types, generic types, shaped arrays, and mixed types. If you’re familiar with TypeScript, you’ll be very familiar with the typing system.

All Hack files are in one of three modes. These modes will determine the strictness of some of the features for the compiler. The default typing mode is partial. Partial will only typecheck when typing is provided, and it can interface with PHP files. Decl mode will only export the function signatures from the file. The last typing mode is strict. In this mode, all functions must be fully annotated, and you can’t call any un-annotated functions. Additionally, you cannot use any superglobals, references, or top-level code.

I hope this post helped clue you into HHVM and Hack and gave you enough background to know if they’re a good fit for your next project.