5

What The Heck Is Hack

Well, first of all it has nothing to do with hacking but it’s a new open-source programming language developed by FaceBook team which is based on PHP and runs on HHVM (Hip Hop Virtual Machine), the FaceBook’s virtual machine which also supports PHP as well.

Hack: a new programming language for HHVM announced by FaceBook

Today we’re releasing Hack, a programming language we developed for HHVM that interoperates seamlessly with PHP. Hack reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages. Read More…

Basically, Heck is an improvement of PHP while PHP is a dynamic-type language and Hack is a combination of both dynamic and static type language because Heck combined the elements of static-type programming languages such as C or C++ with dynamic-type languages like PHP.

Let’s see an example of PHP function:


Now let's see another example of same function in Heck


The difference is pretty straightforward, Heck provides static type casting facility in function parameters and return type but in PHP we can't do it but yes, PHP - 5 introduces type hinting but only using a class/object but we can't type cast any scalar values (int, string etc) and there is no way to type cast the return value of a function or class method in PHP, anyways.

So, this is better to be able to use static type casting and this is the principal addition in Heck along with other features. Facebook team has developed a system to annotate function signatures and class members with type information; the type checking algorithm (the “type checker”) infers the rest. Type checking is incremental, such that even within a single file some code can be converted to Hack while the rest remains dynamically typed. So technically (according to Facebook), Hack is a “gradually typed language: dynamically typed code interoperates seamlessly with statically typed code.

Within Hack's type system, several features has been introduced such as generics, nullable types, type aliasing, and constraints on type parameters. These new language features are unobtrusive, so the code you write with Hack will still look and feel like the dynamic language to which PHP programmers are accustomed.

Why statically typed language is better than dynamically typed language:

Basically dynamically typed languages allows for rapid prototyping, more concise code, and a lot of flexibility. Dynamic typing also comes with a cost. Errors are only caught at run time. There is no compile-time analysis for code optimization. On the other hand, with statically typed languages, errors can be caught before run time. Code becomes more readable and self-explaining and bug free before before it goes to live.

How it works:

Hack is based on HHVM dynamic compilation or just-in-time compilation (JIT) engine. A JIT compiler runs after the program has started and it compiles the bytecode (already compiled to a form of nearly machine code, before runtime) on the fly into machine code.

A common implementation of JIT compilation is to first have AOT (Ahead of Time) compilation to bytecode (virtual machine code), known as bytecode compilation, and then have JIT compilation to machine code (dynamic compilation), rather than interpretation of the bytecode. This improves the runtime performance compared to interpretation, at the cost of lag due to compilation. Read more...

Since one of the main issues with PHP is that, coding errors are not detected until the script gone live, which is too late but, by combining the features of both static and dynamic languages; programmers can retain all the speed they'd have with a dynamic language like PHP, while also it's possible to catch mistakes before runtime and it was done by the "type checker" which runs as a local server and watches the file system. The server keeps all information about the source code in memory and automatically updates itself when a file changes on disk. The type checker typically runs in less than 200 milliseconds and rarely takes more than a second, making it easy to integrate into the development workflow without introducing a noticeable delay.

Heck and PHP code could be run from a single file, Heck code blocks starts with <?hh while PHP uses <?php and also it's possible to call Heck code from PHP code block. Since, Heck and PHP may run side by side but some deprecated features of PHP not supported in Heck and also some other features that don’t work well with static typing.

Hack helps bridge the gap between dynamically and statically typed languages by providing features normally seen in statically typed languages to PHP. The primary goal of Hack was to bring these features but remain as compatible as possible with current PHP codebases. Read more about Heck, it's features with examples, tutorial and documentation on hacklang.org.

Is this a replacement for PHP:

Well, honestly I think, it's been built to replace the PHP, it's primary goal was to use the dynamic nature of PHP but with some improvements such as early error detection is one of those improvements and hence, Heck has just combined the static language feature like type casting with PHP's existing features, it just reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages. In fact, most PHP files are already valid Hack files. Hence, Hack is a programming language for HHVM that interoperates seamlessly with PHP but Heck is still not PHP.

Notice: The Hack has hacked the PHP but hard to replace...

Whether it'll replace the PHP or not, it depends on developers. If developers adopt Heck over PHP then it may replace it but there are so many reasons that developers may not adopt it despite of being an improved language similar to PHP. It only runs on HHVM while PHP runs on many platforms. HHVM still doesn't run on Windows but they are working on it, maybe soon it'll be available for Windows as well. Since most developers use third party libraries and frameworks to build applications/websites using PHP, in this case Heck is a new language and there are no such resources (third party libraries and frameworks) available to work with Heck. Also, hosting providers (in case of shared hosting) have to provide such an environment for Heck to run because without HHVM it won't work but PHP is everywhere. Finally, In my personal opinion, Facebook didn't gain enough trust from developers because of their wildly inconsistent behavior in past, I have experienced some problems too, anyways.

It's true that, Heck has some better features that PHP doesn't have but PHP is also getting better day by day and I believe the PHP developer community is also working hard to improve the language and this is a continuous process so what if PHP adds these features in future versions of it. Since PHP - 6 is released and have some very cool and interesting improvements but is that all PHP got? Definitely not, by the time PHP will improve as well and maybe it'll remain as a superior one, who knows!

As a developer, I definitely like Heck because of it's similarity with PHP while it added new features like other static typed language but only these are not enough to switch from one language to another, developers need more resources and support and a supportive community as well. So, maybe it's not the time to migrate but it's a good idea to give it a try, it doesn't require much effort. If anyone wants to give it a try then at first go to the the home page of Heck to get an overview then navigate to Installation and Configuration from Docs and also check Installation on Cloud Computing platforms, there you can find HHVM installs on the » Heroku cloud platform, just visit that Github page, it's a Heroku Buildpack for HHVM. Just read the instructions and you'll be able to test it, good for Windows users.

If you are not familiar with Heroku then check the Heroku Dev Center, it's very easy to signup and get running. If you don't have an account on Heroku then create one first, it's simple and then follow the simple four steps on Getting Started with Heroku. Just create simple index page and run some Heck code. You may also check the tutorial on Heck website and you may also read Bootstrapping a Project. Happy Hacking...

I would like to hear from you, please share your thoughts, what do you think about the Hack ? Do you think it'll replace the PHP ? Thanks!

N/B: Article based on Heck website.

Latest Blog

0
Php

PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]