Facebook's Hack programming language builds code safety into PHP

Hack pairs PHP's ease with the safety controls of older languages

Facebook has released a programming language called Hack, which marries the ease of PHP with the rigorous safety controls of older languages such as C++.

PHP programmers should easily understand Hack, which replicates many of the same features and functions of PHP, and adds a few of its own for greater productivity, said Bryan O'Sullivan, a Facebook engineer on the project.

Over the past year, Facebook has converted nearly all of its PHP code base to Hack, which makes up the core of its website.

In creating Hack, Facebook took an approach that was similar to Microsoft's with TypeScript, which is basically a superset of JavaScript that, like Hack, adds static typing.

Both projects set out to strengthen a popular dynamic programming language so it can be more easily used by large software teams to design mission-critical applications.

Individuals would also benefit by using Hack, O'Sullivan said, both in terms of increasing performance of their websites and improving the overall quality of their code.

Hack requires Facebook's HHVM (Hip Hop Virtual Machine) to run. HHVM is a virtual machine that compiles PHP, normally an interpreted language, into byte code, so it can run more quickly.

Hack is basically an extension of the PHP language with built-in static typing, a feature found in more traditional programming languages such as C/C++ and Java, O'Sullivan said.

Many of the newer Web-oriented programming languages, such as PHP and JavaScript, do not have static typing, hence they are referred to as dynamically typed languages.

With dynamic typing, "there is no explicit information in the source code that describes what kind of information the program is dealing with," O'Sullivan said.

In contrast, static typing requires the programmer to define the data type for each variable before that program is compiled or run.

Though it takes extra work to implement, static typing prevents run-time errors occurring when the wrong data type is entered into the program, either by human input or some other computer function.

"There are certain kinds of errors and crashes that can occur," if the programmer is not careful about what data is assigned to variables, O'Sullivan said. "These latent errors can hide for a long time in a dynamically typed languages."

The HHVM virtual machine has a built-in type checker to ensure that all of the typed information is correct. Hack even allows the programmer to define unique data types.

"Syntactically, Hack is very close to PHP. We allowed it to be possible to run PHP and Hack code side-by-side so you can gradually convert your language codebase from PHP to Hack," O'Sullivan said.

Certain deprecated PHP features, however, are not supported in Hack, and neither are a handful of features that don't work well with static typing.

Hack also comes with a number of additions not found in PHP. One is Collections, a way to create arrays with more nuance than the array function offered by PHP itself, O'Sullivan said.

Hack also eases the use of closures through the use of Lambda expressions. Closures, which were added to Java 8, "make it easy to succinctly write fairly complicated data transformations," O'Sullivan said.

Hack's Lambda expressions provide a way to create closures "with a fewer number of keystrokes, which is a big win for productivity," he said.

Facebook has supplied a number of text editor plug-ins on the Hack website to help coders write in the language, though the company is hoping volunteers will build a few more elaborate ones.

O'Sullivan didn't reveal any specific plans to offer the Hack augmentations back to the keepers of PHP, though he did note that the company plans to "work closely with the open-source community," to further develop the language.

Joab Jackson covers enterprise software and general technology breaking news for The IDG News Service. Follow Joab on Twitter at @Joab_Jackson. Joab's e-mail address is Joab_Jackson@idg.com

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags Facebooksoftwareapplication developmentWeb services developmentLanguages and standards

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Joab Jackson

IDG News Service
Show Comments

Father’s Day Gift Guide

Brand Post

Bitdefender 2019

Bitdefender solutions stop attacks before they even begin! Get cybersecurity that 500 MILLION users already have and trust.

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?