Friday, August 29, 2014

Hack is a new programming language based on PHP. It is developed by Facebook and was announced recently on March 20, 2014. The syntax is basically the same as PHP, but underneath the hood there are a lot of features such as static typing, native collections, generics and more which PHP has been lacking. Hack allows programmers to use both dynamic typing as well as static typing. Hack is not PHP, as it runs only on Facebook’s HipHop Virtual Machine (HHVM), a competitor to the traditional PHP Zend Engine.

Why was Hack born?

Much of the internal code of Facebook was first written in PHP, since the language is notoriously easy to learn and use. Also, Facebook uses PHP language largely to attract new talent and increase developers efficiency.

However, PHP could not perform as well at a scale as large as Facebook’s. Reason being that PHP is a loosely typed language and type-related errors are not recognized until runtime. In order to solve this and other such problems, Hack was born. Facebook added strict typing feature and runtime-enforcement of return types to the new language. Strict typing nullifies the need for a lot of type-related unit tests and encourages developers to catch type-related errors sooner in the development process.

New Features

Comparison of PHP and Hack
Hack introduced many new features which are not present in the current PHP version. Some of those features were proposed to PHP over time but they were never accepted. These new features include type annotations, nullable types, generics, collections, lambdas and more. Hack takes advantage of HHVM (HipHop Virtual Machine) to execute the code faster.

Most existing code of PHP will run on Facebook’s HHVM engine. This is necessary because Facebook’s existing codebase is largely existing PHP code.

Hack code can be mixed with regular PHP code. To convert a PHP class to a Hack class, change the leading <?php to <?hh. That’s literally all that is necessary. This allows smooth migration between PHP and Hack code, so you can take advantage of Hack's additional features.

Hassan Ahmad

Hassan is a web designer, developer who has been in this field for 6 years. He has special expertise in Java, C++, HTML/CSS, PHP, JavaScript and WordPress.

Newer Post
This is the last post.

0 comments