PHP7 Closure::call()

2016-09-20 0 456
PHP7 Closure::call()
Closure::call() 方法被添加作为临时绑定的对象范围,以封闭并简便调用它的方法。它的性能相比PHP5.6 bindTo要快得多。

示例 – PHP7之前

<?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

示例 – PHP7+

<?php
   class A {
      private $x = 1;
   }

   // PHP 7+ code, Define
   $value = function() {
      return $this->x;
   };

   print($value->call(new A));
?>

这将在浏览器产生输出以下结果-

1

遇见资源网 PHP7归档 PHP7 Closure::call() http://www.ox520.com/12379.html

下一篇:

已经没有下一篇了!

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务