Closure::call()方法是一种快捷方式,用于将对象范围临时绑定到闭包并调用它。与PHP 5.6的 bindTo 相比,它的性能要快得多。
PHP 7之前的版本
<?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 7+版本
<?php class A { private $x=1; } //PHP 7+ code, Define $value=function() { return $this->x; }; print($value->call(new A)); ?>
输出-
1