HHVM和Hack

今天看了四章内容(Testing, Profiling, HHVM-Hack, Community),其他都不提了,今天就提一下 HHVM 和 Hack。

HHVM

php 是一种解释型语言这我们都知道,在将 php 语言转换成机器码时需要解释引擎,在以前,只有一个引擎,Zend Engine,后来呢,Facebook的扎克伯格在大学的时候开发了个网站,因为 php 开发快,所以他一开始就选了这个语言,后来大家也知道,这网站越来越大,公司在已有大量服务器的基础上,决定不再继续采购服务器了,而是改进这门语言的解释器,所以有了我们看见的 HHVM ( Hip Hop Virtual Machine ),据说是快了好几倍。

官方介绍👇

HHVM is an open-source virtual machine designed for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.

HHVM supports Hack, PHP 5 and the major features of PHP 7. We are aware of minor incompatibilities, so please open issues when you find them. HHVM also supports many extensions as well.

怎么说呢,当我在有高性能的需求的时候可能会去使用他,毕竟现在我使用的 php7.1 也是蛮快的。

Hack

Facebook 自己为了需求开发的语言(大公司就是不一样啊,都喜欢自己发明语言。)。Hack 语言可以说是和 PHP 是极为相似的,算是一种方言,最大的区别在于 Hack 是有静态类型的,PHP 都是动态的判定你是整型或者字符,而 Hack 是可以规定类型也可以不规定,可以说是更加灵活了。所以总的来说,Hack 语言是比 PHP 强的,但是呢,PHP 是一直在发展的,有社区在推动,生态也很完整,所以不是有特殊需求一般是不会切换到 Hack 的。不过 Hack 也为 PHP 带来的很多灵感,这也是开源项目带来的优势啊,大家相互借鉴,相互提高,希望以后 PHP 也能吸取其他语言的优点,更好的发展。

官方介绍👇

Hack is a programming language for HHVM. Hack reconciles the fast development cycle of a dynamically typed language with the discipline provided by static typing, while adding many features commonly found in other modern programming languages.

Hack provides instantaneous type checking by incrementally checking your files as you edit them. It typically runs in less than 200 milliseconds, making it easy to integrate into your development workflow without introducing a noticeable delay.

Hack 语法👇

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?hh
class MyClass {
public function alpha(): int {
return 1;
}

public function beta(): string {
return 'hi test';
}
}

function f(MyClass $my_inst): string {
// Fix me!
return $my_inst->alpha();
}