PHP7中Closure :: call的使用范例

PHP7中Closure :: call的使用范例,第1张

PHP7中Closure :: call的使用范例 PHP 7Closure::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);
 ?>

推荐:php视频教程 php7教程

以上就是PHP7中Closure :: call的使用范例的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/680093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存