PHP 7 的 Closure::call() 有着更好的性能,作用:将一个闭包函数动态绑定到一个新的对象实例并调用执行该函数。

 

描述:

public mixed Closure::call ( object $newthis [, mixed $... ] )

暂时将闭包绑定到newthis,并用任何给定的参数调用。

php7之前的示例:

<?php
class A {
    private $x = 1;
  }
// PHP 7 之前版本定义闭包函数代码
    $getXCB = function()
    {
    return $this->x;
    };
// 闭包函数绑定到类 A 上
$getX = $getXCB->bindTo(new A, 'A'); 
echo $getX();
print(PHP_EOL);

php7之后的示例:

<?php
class A {
    private $x = 1;
  }
   $getX = function() {
        return $this->x;
  };
  echo $getX->call(new A);
 ?>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。