Wednesday 9 September 2015

Hack vs PHP

Hack vs PHP

In today’s article we are going to compare Hack and PHP, providing the advantages and also the disadvantages of both Hack and PHP. Now let’s look in to a brief history of both programming languages, lets start with PHP.

What is PHP?

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. As of January 2013, PHP was installed on more than 240 million websites (39% of those sampled) and 2.1 million web servers.Originally created by Rasmus Lerdorf in 1994,the reference implementation of PHP (powered by theZend Engine) is now produced by The PHP Group.While PHP originally stood for Personal Home Page,it now stands forPHP: Hypertext Preprocessor, which is a recursive backronym.[7]
PHP code can be simply mixed with HTML code, or it can be used in combination with various templating engines andweb frameworks. PHP code is usually processed by a PHP interpreter, which is usually implemented as a web server’s native module or a Common Gateway Interface (CGI) executable. After the PHP code is interpreted and executed, the web server sends resulting output to its client, usually in form of a part of the generated web page; for example, PHP code can generate a web page’s HTML code, an image, or some other data. PHP has also evolved to include a command-line interface (CLI) capability and can be used instandalone graphical applications.[8]
The canonical PHP interpreter, powered by the Zend Engine, is free software released under the PHP License. PHP has been widely ported and can be deployed on most web servers on almost every operating system and platform, free of charge.
Despite its popularity, no written specification or standard existed for the PHP language until 2014, leaving the canonical PHP interpreter as a de facto standard. Since 2014, there is ongoing work on creating a formal PHP specification.
[sourced from Wikipedia]

What is Hack?

Hack is the brain child of Mark Zukerberg and his team of developers, Hack was announced back in March of 2014.
Last year, after a brainstorm from three top engineers, Facebook solved the server problem by running all its PHP code on a new software creation called HHVM, short for Hip Hop Virtual Machine. HHVM was a new foundation for the Facebook website, letting the company run its site on less servers.

Hack vs PHP

HHVM is a lot faster and is excellent for use on websites with a large database.
This isn’t about carbon emissions… though, given the fact that HHVM can drastically reduce the number of servers and therefore power requirements, there is an argument that can be made for this too!
It is about the impact on your technical environment. With the release of HHVM 2.3 they added support for the FastCGI interface. This means that if you are currently using php-fpm, HHVM is effectively drop-in.
Additionally, it is possible to run PHP.net and HHVM simultaneously by simply routing some requests to different FastCGI proxies.
For sysadmins, if you are using Ubuntu 12.04/13.10, Debian 7, or Fedora 20, there are packages available directly from the HHVM team.
One of the biggest pain points for developers will be lack of IDE support — specifically for debugging. Some, may also not allow you to set the HHVM binary in-place of the PHP binary for running command line scripts and things like PHPUnit.
Perhaps the biggest deficiency at this point however is the lack of support for standard PHP extensions — this means that if you use a PECL extension, it probably won’t work with HHVM out of the box. The team is actively trying to implement the most common extensions however, and making great progress in this. A list of currently available extensions can be found here.

Extensions

There has been a hugely renewed interest in PHP extensions lately, with tools like Zephir, andPHP-CPP, possibly spurred by the success of HHVM, but undoubtedly by the maturation of the community into the enterprise space over the last decade.
With HHVM, as long as the extension does not wrap a C/C++ library, then you can simply write it in PHP (or Hack). HHVM does such a good job of compiling down to machine code, it can be as fast, if not faster than writing the same thing in C++ yourself.
An example of this, is the community contributed Mongofill which is a Mongo extension replacement for HHVM.
In the case that you need to drop down to C++, HHVM provides the HHVM-Native Interface (HNI), which allows you to stub functions in PHP/Hack, and write the implementations in C++.
For full details on implementing extensions for HHVM, see the HHVM Documentation for the Extension API.

Hack?

While most people think of HHVM as an alternative PHP runtime, it is additionally the runtime for Facebooks own “language” known as Hack. Best described as a syntactical sibling of PHP that supports a number of new features such as scalar type hinting on both arguments and return values, generics, collections, constructor argument promotion, and more besides.
Hack is supported alongside standard PHP syntax, its major feature is in fact as a developer tool — when paired with the HHVM static analysis tool, you can get realtime information on things like type mismatches. Additionally, it allows HHVM to make more informed decisions when compiling the code.
However, once compiled, both Hack and PHP code are identical as far as HHVM is concerned.
Hack and HHVM are a great place to trial new potential features for PHP in general, and we can hope to see a lot of the great features back ported to PHP.net in the future.
Hack code starts with a <?hh open tag, rather than the standard <?php open tag, and does not use a closing tag. Hack cannot be interspersed with HTML like regular PHP (great for separation of concerns!) but does support native XHTML as a first-class syntax.

No comments:

Post a Comment