Pinterest Stumbleupon Whatsapp
Ads by Google

10 years ago, in a Harvard dorm room, Facebook was launched. Initially, it was just Mark Zuckerberg hacking away at a codebase which consisted exclusively of PHP code How Does Facebook Work? The Nuts and Bolts [Technology Explained] How Does Facebook Work? The Nuts and Bolts [Technology Explained] Read More , interacting with a MySQL database.

Since then, it has ballooned in size, both in terms of users and developers working on a codebase which is constantly growing. It soon became apparent that whilst PHP was a great language to start Facebook with, it was no longer suited the needs of the company.

And thus they created Hack, which is a purpose built language allowing for faster development, larger development teams, whilst maintaining full interoperability with the popular PHP programming language.

The reception of Hack by the development community has been nothing short of stunning. People are hugely excited about a language that is fast, yet easy to develop whilst maintaining backwards compatibility with the many PHP libraries in existence right now.

Here’s everything you need to know about installing Hack, as well as how to get your feet wet with the language.

What’s So Special About Hack?

Great question. You probably know that some programming languages are compiled to byte code which run on a special virtual machine (like Java and Clojure), whereas other languages (like PHP, Ruby and Python) run in an interpreter.

Ads by Google

You also probably know that interpreted languages are inherently slower than languages which produce byte code which is specifically optimized for the virtual machines they run on. It’s for this reason why there are dialects of Python and Ruby (called Jython and JRuby respectively) which run on the Java Virtual Machine What Is the Java Virtual Machine & How Does It Work? What Is the Java Virtual Machine & How Does It Work? Though it isn't strictly necessary to know it works in order to program in Java, it's still good to know because it may help you become a better programmer. Read More .

Hack uniquely runs on its own virtual machine, called the Hip Hop Virtual Machine. It is this VM which has been used by Facebook to scale for billions of daily users.

Hack is more than the VM upon which it runs upon. It also comes with type annotations, allowing you to declare variables based upon the content which they will store (string, integer, boolean), lambda (anonymous) functions as well as generics.

Installing Hack

I’ve got some bad news for OS X and Windows users. Hack either doesn’t work on these platforms, or support is so flaky it isn’t worth discussing. Instead, you’re either going to have to fire up a Linux VPS Learn All About Virtual Private Servers In Two Minutes Learn All About Virtual Private Servers In Two Minutes With so many great web hosting services available, it's hard to decide the right one to suit your needs. Read More or VM.

hack-ssh

Out of sheer laziness, I ended up settling on creating a Linux VPS with Digital Ocean, who are one of my favorite VPS providers. I created a small droplet running Ubuntu 13:10 Ubuntu 13.10 Launched With Friends Scope, Dash Filters and More Ubuntu 13.10 Launched With Friends Scope, Dash Filters and More Canonical has launched the latest and great version of the world’s most popular free and open-source desktop operating system, Ubuntu 13.10 “Saucy Salamander”. Read More , and then installed Hack with the following commands.

wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/ubuntu saucy main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install hhvm-nightly

As you can see here, I download the GPG key for the Hack repositories; add the repository to my sources list; update my sources definitions; upgrade my system and then install Hack. Simple, really.

Hacking With Hack

Now, it’s time to write some Hack code. We’ll start off with the tried and tested ‘Hello World’ program. This one will not run in a web browser, but rather print ‘Hello World’ to the console.

Create a new file called HelloWorld.php (yes, for some reason Hack insists on using .php as its default file extension) in your favorite text editor (my preferred choice is VIM The Top 7 Reasons To Give The Vim Text Editor A Chance The Top 7 Reasons To Give The Vim Text Editor A Chance For years, I've tried one text editor after another. You name it, I tried it. I used each and every one of these editors for over two months as my primary day-to-day editor. Somehow, I... Read More ) and add the following lines.

hack-hello

<?hh
 echo "Hello World";

Which should print out Hello World to the console when ran with ‘hhvm’, like so.

hack-hello-run

Alright, let’s try and run a Hack program in the browser. First, we’re going to need to install Apache and PHP. I’ve touched on this in a previous article Signed Up for SSH-only Web Hosting? Don't Worry - Easily Install Any Web Software Signed Up for SSH-only Web Hosting? Don't Worry - Easily Install Any Web Software Don’t know the first thing about operating Linux through its powerful command line? Worry no more. Read More , but to refresh your memory, I’ll run through it again. Run the following commands.

sudo apt-get install apache2
sudo apt-get install php5

You can also install Apache, PHP, MySQL and a whole bunch of other useful utilities with the following command. (Don’t forget the caret at the end of the line – lamp-server is not a single package, but rather a collection)

sudo apt-get install lamp-server^

You are recommended to run this if you plan to take a closer, more serious look at web development with the Hack programming language, as it contains a lot of tools which you might find quite useful.

Once you’ve done that, restart the Hip Hop VM with the following command.

sudo /etc/init.d/hhvm start

Check that Apache is running by opening a browser and navigating to the IP address of your web server. If everything is running, you should see this.

hack-itworks

Great! Now, navigate to /var/www and remove the page you just saw (called ‘index.html’) with the following commands.

cd /var/www
rm index.html

Now, create a file called index.php and add the same lines you wrote before.

<?hh
 echo "<p>Hello World</p>";

Once you’ve done that, revisit your web server with your chosen web browser.

hack-helloworld-browser

As you can see, there’s some weirdness with Hack recognizing the end of a string. As a result, I’d like to take this opportunity to remind you that whilst this technology is quite cool, it’s still pretty raw, and possibly not ready for production deployments. However, I will add that I am running the nightly version of Hack, which is the most bleeding edge version available. As a result, it shouldn’t be too surprising that some things don’t run perfectly well.

If you know PHP, you might recognize the ‘Echo’ statement I used before. Well, Hack can call any PHP function. Here I am calling ‘phpinfo();’ within a Hack program.

hack-phpinfo

Some Hack Specific Functionality

Hack brings a whole lot to the table, with respect to new language features. I discussed some of them before, including type declarations. Sadly, we’re not going to be able to cover everything in this one article, but I figured it might be a good idea to look at how Hack handles type definitions.

So, to recap, type definitions are where you define a variable based upon the contents it would hold. How does that work? Well, a bit like this.
hack-types


<?hh
 bool $trueorfalse = false;
 string $myname = 'Matthew Hughes';
 int $myage = 22;

As you can see, we start off with the type declaration, followed by the name of the variable (starting with a dollar sign, much like in traditional PHP), followed by the value assigned to the variable.

This also comes into play with function declarations. When you’re declaring a function and passing it a parameter, you have to declare the type of variable you’ll be passing into it. If you don’t, or pass in the wrong type of variable, expect wailing and gnashing of teeth.

So, how does this actually work in Hack? Let’s find out.

Create a new file called ‘function.php’ and write the following lines.

hack-function


<?hh
function hello (string $yourname): void {
 echo 'Hello ' . $yourname;
}
hello('dave');

If you’ve used PHP in the past, you might be able to decipher some of this. We’ve created a function, which we’ve called ‘hello’. We then pass it a string, which is then echoed to the console, following the word ‘Hello’.

But what’s that : void bit? Well, in Hack, we have to tell the computer whether the function will be returning a value. If it isn’t, we say that the function is ‘void’. As everything with Hack, we have to declare the type of values that will be returned.

We then run this code with the ‘hhvm’ command line application, and we should see this.
hack-hhvm-hello-dave

Conclusion

There’s a lot to love about Hack.

It’s a language which mandates you to write better code, whilst being easy to understand and ridiculously fast. It also has the support of one of the largest technology companies around right now, who use it daily in production to make one of the largest sites in existence scale to millions of users.

Despite that, it’s still a very new language. In fact, I wrote this article the day after it was released to the public. It’s hard to recommend that anyone starts putting it into production right now, but certainly play around with it.

But that’s just my opinion. Will you be giving it a try? Drop me a comment below and let me know what you think.

Photo Credit: Book (Jixar)

  1. Julien Verlaguet
    March 24, 2014 at 6:08 pm

    Hi, designer of Hack here,

    A bit of context on a couple of questions that were raised, I am not trying to give an opinion here, just context:
    "Not ready for production? According to the blog post, most of Facebook’s production code in written in Hack, running on the HHVM. "

    This is true. Hack is in production, and most Facebook codebase runs on it.
    However, Hack was designed *for* Facebook.
    For years, the PHP codebase at Facebook has been improved. We had various tools (lint, checkers etc ...) that were checking the codebase way before Hack came to be.
    With that in mind, the "style" of PHP that we had at Facebook had been drastically polished way before Hack.
    I suspect that there are a lot of "Facebookisms" still present in Hack. I hope we will be able to correct those one by one, by listening to the feedback coming from users. I can assure you everybody on the team is doing that full time right now. Listen to feedback, make Hack a great experience for everyone.

    " Why do companies think that it’s a good idea to choose a totally generic name for a unique product?"

    We, the engineering team are to blame for this one, not the company. The name is the outcome of a vote that took place a while ago (~ 2 years).
    At the time, the project was much smaller and we just needed "a name".
    As the team grew, and more and more Hack code was getting in the codebase, the name spread in the company. The question was raised to change it, but I think we all got use to it and started to like it :-)
    It's true it's hard to search. But then again, so is D, C, go, etc ...

    • David B
      March 27, 2014 at 10:28 am

      You could always call it dot.hack :)

  2. Chris
    March 24, 2014 at 1:34 pm

    So, first off, you're not actually running Hack via Apache, which is why you got the weird string outputs. You need to configure Apache to proxy back to the HHVM FastCGI instance (and run HHVM as FastCGI instead of the full web server mode, they are two different things). What you did here was just parse a plain file. The reason it "looks" mostly OK is because the tags are hidden, yet broken. Check your source.

    Also, in your phpinfo() call, that's the default PHP information provided by the packages that come with Ubuntu. That's not HHVM.

    • Joshua J
      March 25, 2014 at 6:11 am

      i agree with this assessment.

  3. Stephan H
    March 23, 2014 at 11:56 pm

    Here's a general rant, not specific to but concerning "Hack": Why do companies think that it's a good idea to choose a totally generic name for a unique product? It makes looking for help on the subject a hell on the internet. "Hack" is one Example. Apples' "mail" is another. Or how about the idiotic decision to name apps "Discs" or "Files" (I'm looking at Gnome here). I hate this trend from the bottom of my heart and would tar and feather the people who made these decisions if I could.

    • Matthew H
      March 24, 2014 at 12:01 am

      Or Google's Go? Virtually ungooglable, ironically enough.

  4. Tom W
    March 23, 2014 at 9:20 pm

    If I do some more web dev in the future, I'll probably give this a go. Ever since I started Android dev, it's annoyed me that PHP doesn't have type declararions.

    • Matthew H
      March 24, 2014 at 12:02 am

      It's good fun. If you've got a spare afternoon, I'd totally encourage you to download HHVM and play around with it.

  5. Craig Bass
    March 23, 2014 at 8:56 pm

    I don't understand why you needed to install php5, hhvm replaces this.

    • Matthew H
      March 24, 2014 at 12:00 am

      The documentation says that you need to have it installed! :(

  6. Jeremy Banks
    March 23, 2014 at 8:46 pm

    > As you can see, there’s some weirdness with Hack recognizing the end of a string. As a result, I’d like to take this opportunity to remind you that whilst this technology is quite cool, it’s still pretty raw, and possibly not ready for production deployments.

    Not ready for production? According to the blog post, most of Facebook's production code in written in Hack, running on the HHVM. Hack might not have been used outside of Facebook yet, but that's a huge deployment itself, and HHVM has been used in production by several companies.

    I think you're wrong about what happened in your example, and that it was your mistake, not that of the language. What you see in the screenshot is exactly what you'd see if you opened that file in a browser and the PHP/Hack/HHVM engine wasn't used. Give it a .html extension and you'll see what I mean. Maybe the server's configuration change hadn't kicked in, or you accidentally opened the file directly instead of going through the server.

    • Matthew H
      March 24, 2014 at 12:07 am

      No, I totally went through the server. And I was running the HHVM. Weird. I'll give it another play around with in a few days.

      Cheers for the comment!

Leave a Reply

Your email address will not be published. Required fields are marked *