Start coding with Hack

2 minute read

If you like to take your PHP to the next level there is an excellent option for you: Hack. It is a programming language develop by Facebook. They have been using it for a couple of years now. In the spring of 2014 they decided to remove all ”Facebook stuff” from Hack and release the language as open source.

So you are probably wondering why Hack is a good thing. What can Hack do that PHP can’t? The truth is that it is not much Hack can do that PHP can’t… But there are some features that will make you a better developer. Hack is a static typed language. That means that you have to declare a type of each variable, function parameter and return values. Like Java or C#. This is good because it helps you find bugs while you are writing the code!

Install Hack

Hack does only run on HHVM. It is also developed by Facebook and will run Hack and PHP really fast! If you want to test Hack I recommend using vagrant and this repo.

  1. Install VirtualBox
  2. Install Vagrant
  3. Clone the repo into /some/path/hhvm
  4. Edit your /etc/hosts file to include hhvm.local with IP
  5. Open a terminal and go to /some/path/hhvm
  6. Run vagrant up (it will take a while)

When the vagrant up has finished downloading the virtual machine and installed everything, go to http://hhvm.local in your browser. If you see “Hello Hack!” you know everything works.

Look at the source folder. You find a file called index.hh. It contains a very complicated way of printing a text string. I’ve written it like that to show you about generic types. You may make a change to the index.hh and then see your changes in the browser directly. In this way, Hack works just like PHP. You don’t need to compile anything.

Finding bugs

I said Hack was great to find bugs.With some help from the command hh_client you will get detailed error messages when you’ve done something wrong.

$ cd /some/path/hhvm
$ vagrant ssh
$ cd /vagrant/source
$ hh_client
No errors!
$ nano index.hh
# change “getAStore(): Store<string>” to “getAStore(): Store<int>”
$ hh_client
/vagrant/source/index.hh:23:12,13: Invalid return type
/vagrant/source/index.hh:19:29,31: This is an int
/vagrant/source/index.hh:21:13,25: It is incompatible with a string

What happened here is that we said getAStore should return a Store of integers but instead we returned a Store of strings. Hack sees that we have done something wrong and gives us a warning.

There are editor plugins for hh_client for Vim and Emacs. It’s only a matter of time until someone builds plugins for sublime or PHPStorm.

Categories: ,


Leave a Comment