Closure::call() 方法,作为临时将对象作用域绑定到闭包并调用它的简便方法添加。它的性能比 PHP5.6版本。
<?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
<?php
class A {
private $x = 1;
}
// PHP 7+ code, Define
$value = function() {
return $this->x;
};
print($value->call(new A));
?>
它产生以下浏览器输出–
1