At Facebook scale the limits of PHP bit hard: thousands of engineers shipped new code twice a day, yet calling a method on a null object or guessing a complex API's method name produced errors only at runtime, and the language gave tools little structure to introspect. The dynamic language kept the edit-and-refresh loop fast but offered no early safety net, while a conventional statically typed language would have bought checks at the cost of iteration speed — and a rewrite would have thrown away Facebook's investment in PHP and its libraries.

Hack, announced on March 20, 2014 as part of the HHVM runtime, squared that circle by staying a superset of PHP. Most PHP files are already valid Hack, so an engineer could add type annotations to one function and leave the next dynamic, with statically and dynamically typed code interoperating seamlessly even inside a single file. Hack added generics, nullable types, type aliasing, collections and lambda expressions, and HHVM enforced annotated return and parameter types at runtime, which also let its JIT safely trust annotations for optimization.

To keep the fast PHP workflow intact, the team architected the type checker as a local server that watches the filesystem and keeps the whole code graph in memory; typical checks ran in under 200 milliseconds and rarely exceeded a second. Because conversion was granular and opt-in, engineers converted the majority of their own code voluntarily, homegrown code-modification tools automated the rest, and over the previous year nearly the entire Facebook PHP codebase had migrated to Hack.

The same announcement open-sourced Hack and the conversion tools under the HHVM umbrella, which would keep running unmodified PHP beside Hack, and invited developers to a Hack Developer Day on the Menlo Park campus the following month. The release drew 576 points and 415 comments on Hacker News the day it appeared.

Bolt gradual static typing onto PHP itself so safety spreads function by function, never all at once

Superset compatibility meant no rewrite and no big-bang migration: existing PHP files kept running, HHVM stayed a PHP runtime, and third-party PHP libraries remained usable throughout.

Per-function granularity let safety spread at each team's own pace, because unannotated code stayed dynamic instead of blocking an entire file or module from converting.

A filesystem-watching checker server kept feedback under 200 milliseconds, so static typing did not tax the edit-and-refresh loop that made PHP iteration fast.

Engineers chose to annotate their own code and codemods handled the repetitive rest, so a multi-million-line codebase converted within a year rather than waiting on a dedicated migration team.

Don't rewrite to gain safety: make the language accept old code, let the new discipline spread one function at a time, and keep the check fast enough that it never interrupts the workflow.

Hack and its code-modification tools were open-sourced at hacklang.org as part of HHVM, and Facebook said HHVM would remain a PHP runtime working toward PHP-5 parity so third-party PHP code kept running alongside Hack. The announcement drew 576 points and 415 comments on Hacker News on March 20, 2014, and Facebook planned a Hack Developer Day on its Menlo Park campus for the following month. Inside Facebook the migration was already effectively complete: nearly the entire PHP codebase ran Hack after roughly a year of organic adoption plus automated refactoring tools.

FOLLOW THE EVIDENCE

The sources

  1. Hack: a new programming language for HHVM engineering.fb.com
  2. Hacker News discussion: Hack: a new programming language for HHVM news.ycombinator.com