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.

 

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:

<?hh
 class MyClass {
  public function alpha(): int {
   return 1;
  }
  public function beta(): string {
   return 'hi test';
  }
 }
 function f(MyClass $my_inst): string {
 // Fix me!
  return $my_inst->alpha();
 }

 

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? 

Probably, but Hack isn't exactly a new language it is more an upgrade to PHP. It plays the same role  CoffeeScript plays to JavaScript. These are modest changes that seem to be well thought out and really do make PHP better. Of course they could have just been added to PHP to create PHP 6 or 7. Who knows, perhaps they will be.

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. 

 

hack

 

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...

More Information

http://hacklang.org/

Hack: a new programming language for HHVM

Related Articles

The Top Languages Of 2013       

The Reason For The Weird PHP Function Names       

New - PhpStorm 7       

HipHop VM - Facebook's PHP Virtual Machine    

PHP 5.5.0 Released       

Zend Optimizer+ For PHP?

Zend Survey of PHP Developers       

  

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


Inbox - An API To Modernize Email
09/07/2014

You might think that there enough mail APIs already, but Inbox has another one for you and its approach to creating a universal email system is clever.



Amazon Mobile Analytics
14/07/2014

AWS customers can now use Amazon Mobile Analytics to monitor app usage and revenue from apps within the time frame of around an hour to get started. With its pay-as-you-go model and free startup tier  [ ... ]


More News

Last Updated ( Tuesday, 25 March 2014 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.