PHP クロージャ::call...LOGIN

PHP クロージャ::call()

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

<?php
class A {
    private $x = 1;
}

// PHP 7 之前版本定义闭包函数代码
$getXCB = function() {
    return $this->x;
};

// 闭包函数绑定到类 A 上
$getX = $getXCB->bindTo(new A, 'A'); 

echo $getX();
echo "<br/>";

// PHP 7+ 代码
$getX = function() {
    return $this->x;
};
echo $getX->call(new A);
?>

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

1
1
次のセクション
<?php class A { private $x = 1; } // PHP 7 之前版本定义闭包函数代码 $getXCB = function() { return $this->x; }; // 闭包函数绑定到类 A 上 $getX = $getXCB->bindTo(new A, 'A'); echo $getX(); echo "<br/>"; // PHP 7+ 代码 $getX = function() { return $this->x; }; echo $getX->call(new A); ?>
コースウェア