早起的版本如 PHP5.6 ,綁定並調用閉包使用 bindTo,而PHP7 中 Closure :: call()方法具有更好的性能,廢話不多說,
較早的 PHP 示例:
<?php class Person { private $name = '喬峰'; } $getName = function() { return $this->name; }; // Bind a clousure $value = $getName->bindTo(new Person, 'Person'); print($value());// 喬峰
PHP7 示例:
<?php class Person { private $name = '喬峰'; } $getName = function() { return $this->name; }; print($getName->call(new Person));//喬峰 注意 這里可以直接是 new person,當然new person()也可以