14. Hack Features
Nullable
null を許容するかどうかを?で指定可能
function nullable_arg_func(?string $name) : string {
if ($name === null) return "name is null";
return "Hello " . $name;
}
function null_ng_arg_func(string $name) : string {
return "Hello " . $name;
}
nullable_arg_func(null);
null_ng_arg_func(null); // エラー発生
実行結果
$ hhvm examples.php
Fatal error: Argument 1 passed to null_ng_arg_func() must be an instance of string,
null given in /home/yone098/examples.php on line 10
15. Hack Features
Generics
C++, Java, C# のようなGenrics が利用可能
class Dog<T> {
private ?T $name;
public function __construct(T $name) {
$this->name = $name;
}
public function get_name() : T {
return $this->name;
}
}
$dog = new Dog("garsue");
echo("dog->get_name()=" . $dog->get_name() . "n");
$map = Map<string, int> { "a" => 1, "b" => 2 };
var_dump($map);
実行結果
dog->get_name=garsue
object(HHMap)#2 (2) {
["a"]=> int(1)
["b"]=>int(2)}
24. Tool
hh_client
不正な箇所がある場合に行番号と内容を表示
bad.php
<?hh
function bad_func() : int {
return "Hello, BpStudy#81";
}
bad_func();
実行結果
$ hh_client
/home/yone098/bad.php:3:10,28: Invalid return type
/home/yone098/bad.php:2:23,25: This is an int
/home/yone098/bad.php:3:10,28: It is incompatible with a string
プロジェクトのルートフォルダに.hhconfig ファイルを設置
しチェックを行うのがベスト
34. Performance
wordpress ab
wordpress nginx + php-fpm
Requests per second: 19.53 [#/sec] (mean)
Time per request: 2560.771 [ms] (mean)
Time per request: 51.215 [ms] (mean, across all concurrent requests)
Transfer rate: 154.85 [Kbytes/sec] received
wordpress nginx + fastcgi + hhvm
Requests per second: 42.97 [#/sec] (mean)
Time per request: 1163.692 [ms] (mean)
Time per request: 23.274 [ms] (mean, across all concurrent requests)
Transfer rate: 340.33 [Kbytes/sec] received