|Facebook Introduces Hack, A Better PHP|
|Written by Mike James|
|Friday, 21 March 2014|
Facebook is almost certainly the biggest site running on PHP and now it has introduced its own version of the language and appropriately enough it's called Hack.
Facebook makes heavy use of PHP and in many ways the company is a big enough user to be the dog that wags the PHP tail.
Put simply Facebook needs PHP to be better than it currently is to help make the site more efficient and scale better.
To this end Facebook has been releasing internal tools as open source code that makes PHP better in a number of ways. The first and perhaps best known is HipHop or HPHPc a PHP to C compiler. A little later on it moved on to the HipHop Virtual machine, or HHVM, and deprecated HPHPc and its associated project. HHVM was a fast Just In Time JIT compiler for PHP 5.4.
HHVM started out as a fast runtime for conventional PHP, but it also allowed Facebook to extend the language. Now the extension has gone so far that it is a new language, Hack, that runs on the HHVM. The key additional feature of the language is that it is statically typed. The latest PHP has type hinting, but only for a very restricted range of types.
Hack has been in use at Facebook long enough for the the entire codebase to have been moved across to it and now there is an open source version complete with HHVM. There are also some tools included to make conversion easier.
Hack builds on PHP and most existing PHP programs will work as Hack programs, but there are some functions and features that have been removed. To convert a program into a more appropriate form of Hack you have to add typing, but you can do this in a gradual manner as static and dynamically typed code can be mixed together. If you specify a type for a parameter, say, then the type rule is enforced. If you don't specify a type then it is treated as a standard dynamic type in the usual PHP style.
A small example of Hack and you will get the idea very rapidly:
Notice that the opening tag is <?hh and you don't need a closing tag. You also can't mix HTML into a Hack program, but you can use XHTML.
A big problem is creating a type checker that doesn't slow down the process of interactively writing code and testing it in a web page. The solution adopted is to use a type checker as a separate server. This keeps the type information in memory and watches the file system for changes to Hack files. It is claimed this results in only an extra 200ms to 1s delay to the workflow.
The type checking has also been extended to runtime checking of parameter and return types. This also helps HHVM produce better JIT code.
Generics are also supported - of course PHP doesn't need generics because it is is all dynamically typed. The idea is that you can define a class or function with a parameter determining the type of the variables use. You also get nullable types to make some code easier to write without crashing because something doesn't exist.
As well as optional static typing, Hack has some other new things, inclding a new set of collections that work with static typing and with generics. Every language designer's favorite, Lambda expressions, have also been added and are claimed to be better than PHP closures because they automatically work out what variables need to be included in the closure - PHP proper demands that you name the variables in the enclosing scope. Of course the Lambda expressions have been used within the collections to make everything work more easily.
One really important feature that hardly gets a mention in the announcement is that Hack supports a C#-style async command. You can now declare functions as async and then call them with await - this is the best way to do asynchronous programming.
Other features that are worth noting are: continuations in the form of yield to make iterators easy; traits aka interfaces with implementation to make multiple inheritance easy; tuples; override (like Java); and a few other things.
So has Facebook succumbed to the vanity that drives many a big company to create its own language?
Facebook can't really move away from PHP, but it can improve the language and upgrade it incrementally.
You can download Hack and get started using it very easily - there is even an interactive tutorial. Facebook is also organizing a Hack Developer Day to promote the language.
One final thought - I think that Facebook will regret calling the language Hack. It makes it far too easy a target for everything but a Google search.
Can you hack Hack; what do you call a Hack programmer; Hack is a hack; hack that Hack; and so on...
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Tuesday, 25 March 2014 )|