Latest web development tutorials

PHP Closure::call()

PHP 7 新特性 PHP 7新特性

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 ();
print( PHP_EOL );

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

以上程序執行輸出結果為:

1
1

PHP 7 新特性 PHP 7新特性