與 PHP5.6 的 bindTo 相比,PHP7 中的 Closure :: call()方法具有更好的性能,該方法被添加為臨時(shí)將對(duì)象范圍綁定到閉包并調(diào)用它。
<?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());
?>
它產(chǎn)生以下瀏覽器輸出:
1
<?php
class A {
private $x = 1;
}
// PHP 7+ code, Define
$value = function() {
return $this->x;
};
print($value->call(new A));
?>
它產(chǎn)生以下瀏覽器輸出:
1
更多建議: