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!
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.
- Install VirtualBox
- Install Vagrant
- Clone the repo into /some/path/hhvm
- Edit your /etc/hosts file to include hhvm.local with IP 192.168.99.99
- Open a terminal and go to /some/path/hhvm
- 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.
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.
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.