Closure::call() 方法被添加作為臨時綁定的對象範圍,以封閉並簡便調用它的方法。它的性能相比PHP5.6 bindTo要快得多。
示例 - PHP7之前
<?php
class A {
private $x = 1;
}
// Define a closure Pre PHP 7 code
$getValue = function() {
return $this->x;
};
// Bind a clousure
$value = $getValue->bindTo(new A, 'A');
print($value());
?>
這將在流覽器產生輸出以下結果-
1
示例 - PHP7+
<?php
class A {
private $x = 1;
}
// PHP 7+ code, Define
$value = function() {
return $this->x;
};
print($value->call(new A));
?>
這將在流覽器產生輸出以下結果-
1
上一篇:
PHP7匿名類
下一篇:
PHP7過濾unserialize()
