As every PHP developer should be aware of the Programming Language developed by Facebook : Hack.
It is basically based on PHP, you can say PHP but a strongly / statically typed language like Java.(i.e. a language which have fixed datatype).
Let me give you a simple example.
In normal PHP ,we have functions like
<?php
function addOne($num1){
return ($num1+1);
}
?>
But in hack this code will be changed to
<?hh
function addOne(int $num1) : int{
return ($num1+1);
}
You must have notice the major difference. Inside function parameters, in HACK, we have mentioned int as datatype and int as return type.
This is one of major difference between PHP and Hack.
But this is not the only reason, Why Hack actually evolves??
Basically, We have an advantage in PHP that it need not to be compiled(unlike java), just make changes in file and reload the browser. Languages like Java are slow in sense that they first need to be compiled, then they can be run, but they are easier to manage if the code structure is vast because of Strongly typed as errors can be detected at compile-time.
So Hack does both of the thing at once, It is a statically typed / strongly typed language and is fast that it need not to be compiled after making changes. Just change the code and run the browser,
Hack basically runs on HHVM(Hip-Hop Virtual Machine).
And no one can say anything about the performance of Hack, as world's largest social media site(Facebook) is running on it.
And the biggest advantage of using Hack is it lets coders use both dynamic typing and static typing. This is what’s called gradual typing
As a programmer, you must be wondering what are the differences between Hack and PHP?
I am not going in detail in this blog but i will give some idea about this.
a) First of all, PHP code starts <?php while Hack code starts with <?hh.
b) PHP code ends with ?> while Hack code don't have ending tags.
c) Hack,as I have already mentioned adds datatype to your variables.
d) Hack introduces new collection types (Vector, Set and Map)
Consider this example,
function demo(): int {
$v = Vector {10, 11, 12};
$sum = 0;
foreach ($vector as $value) {
$sum = $sum + $value;
}
return $sum;
}
Basically more datatypes are added and using Vectors are efficient as compared to arrays.
e) Generics concept have been added(Consider same as in Java).
f) Concept of shapes, XHP(for xml or html building)
g) You cannot embed hack code with html(i.e. a page cannot have hack and html code).
That's all for this blog. Very soon I will posting another blog for Installation of Hack and some practical examples on Hack.
If any reader have any suggestion, please comment. Thanks :)
You can also check our Microservices post
Introduction to Microservices
It is basically based on PHP, you can say PHP but a strongly / statically typed language like Java.(i.e. a language which have fixed datatype).
Let me give you a simple example.
In normal PHP ,we have functions like
<?php
function addOne($num1){
return ($num1+1);
}
?>
But in hack this code will be changed to
<?hh
function addOne(int $num1) : int{
return ($num1+1);
}
You must have notice the major difference. Inside function parameters, in HACK, we have mentioned int as datatype and int as return type.
This is one of major difference between PHP and Hack.
But this is not the only reason, Why Hack actually evolves??
Basically, We have an advantage in PHP that it need not to be compiled(unlike java), just make changes in file and reload the browser. Languages like Java are slow in sense that they first need to be compiled, then they can be run, but they are easier to manage if the code structure is vast because of Strongly typed as errors can be detected at compile-time.
So Hack does both of the thing at once, It is a statically typed / strongly typed language and is fast that it need not to be compiled after making changes. Just change the code and run the browser,
Hack basically runs on HHVM(Hip-Hop Virtual Machine).
And no one can say anything about the performance of Hack, as world's largest social media site(Facebook) is running on it.
And the biggest advantage of using Hack is it lets coders use both dynamic typing and static typing. This is what’s called gradual typing
As a programmer, you must be wondering what are the differences between Hack and PHP?
I am not going in detail in this blog but i will give some idea about this.
a) First of all, PHP code starts <?php while Hack code starts with <?hh.
b) PHP code ends with ?> while Hack code don't have ending tags.
c) Hack,as I have already mentioned adds datatype to your variables.
d) Hack introduces new collection types (Vector, Set and Map)
Consider this example,
function demo(): int {
$v = Vector {10, 11, 12};
$sum = 0;
foreach ($vector as $value) {
$sum = $sum + $value;
}
return $sum;
}
Basically more datatypes are added and using Vectors are efficient as compared to arrays.
e) Generics concept have been added(Consider same as in Java).
f) Concept of shapes, XHP(for xml or html building)
g) You cannot embed hack code with html(i.e. a page cannot have hack and html code).
That's all for this blog. Very soon I will posting another blog for Installation of Hack and some practical examples on Hack.
If any reader have any suggestion, please comment. Thanks :)
You can also check our Microservices post
Introduction to Microservices
Comments
Post a Comment