PHP Closure::call()

PHP 7 の Closure::call() はパフォーマンスが向上し、クロージャ関數(shù)を新しいオブジェクト インスタンスに動(dòng)的にバインドして関數(shù)を呼び出します。

Instance

Instance

<?php
class A {
    private $x = 1;
}
// PHP 7 之前版本定義閉包函數(shù)代碼
$getXCB = function() {
    return $this->x;
};
// 閉包函數(shù)綁定到類 A 上
$getX = $getXCB->bindTo(new A, 'A'); 
echo $getX();
print(PHP_EOL);
// PHP 7+ 代碼
$getX = function() {
    return $this->x;
};
echo $getX->call(new A);
?>

上記のプログラム実行出力結(jié)果は次のとおりです:

1
1