April 28, 2014 - Comments Off on Is Hack the future of PHP?
Is Hack the future of PHP?
The words ‘Hack’ and ‘Facebook’ are not two you’d normally want to hear together, but that’s all about to change.
About a month ago Facebook released a new programming language called Hack. Internally, Facebook has been using Hack for a little while now, and over the last year they have converted almost all of their codebase from PHP to Hack.
So should you make the move too? Let’s look at what Hack brings to the table.
Static typed language
The main change from PHP to Hack is that Hack is a statically typed language. This means that the developer must specify a type for all of the variables they use within their code.
This is a feature more familiar with a compiled language such as C++ as opposed to PHP, which is dynamically typed. Dynamically typed languages don’t require types to be specified – instead the types are inferred at run time.
Dynamically typed code is often seen in scripting languages that are designed for the web, as there is no compilation step and you can simply refresh your webpage to see the changes that you’ve made. This “fast feedback loop”, as Facebook calls it, is something they wanted to maintain when developing Hack.
As such, the type checker within Hack works by running as a daemon in the background, and when a change to a Hack file is made, type and syntax errors are reported directly within the code editor. Due to the speed of the type checker, Facebook reports it takes less than 200ms to run on an everyday script. This process happens extremely quickly to help the developer work in the same “fast feedback loop”.
The benefit of having statically typed variables is that it can help pick up errors before you even move into the browser for testing. As the type checker is running every time you save a file, it’ll prompt you to say that you’ve made a mistake – hopefully saving you time in the long run.
PHP files are already Hack files
While Hack is statically typed, you’re not forced to use it. There are two ‘modes’ in which you can run a Hack file: strict and gradual.
When you change the PHP start tag, <?php, to the Hack start tag, <?hh, you’re automatically putting the file in gradual mode. This means that if types are specified, the type checker will check those – but if you don’t specify a type, it’ll act in the same way a dynamically typed language does.
To force static typing in your Hack files you need to enter strict mode. To do this you simply add //strict after the opening Hack tag, e.g. <?hh //strict.
More to discover
As well everything discussed above, Hack also brings support for Collections, Shapes, Type Aliasing, Async and more. For further detailed information on these I highly recommend you take a look at the Hack docs.
Personally I can see the appeal of using Hack on large projects, but for smaller projects and one-page scripts I can’t see any need for the extra overhead in setting Hack up. Facebook says it is committed to supporting Hack now and in the future, and as they have converted almost all of their codebase I can’t see them moving away from it any time soon.
With such a large company behind the project I can see Hack becoming very popular in the future, but I can’t see it replacing PHP in the immediate future.
For more information, visit the Hack website.