PHP Closure::call()

PHP 7 新特性 PHP 7 新特性

PHP 7 的 Closure::call() 有著更好的性能,將一個閉包函數(shù)動態(tài)綁定到一個新的對象實例并調(diào)用執(zhí)行該函數(shù)。

實例

實例

<?php
class {
    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);
?>

以上程序執(zhí)行輸出結(jié)果為:

1
1

PHP 7 新特性 PHP 7 新特性