PHP 7 - Closure :: call()

将Closure :: call()方法添加为临时将对象范围绑定到闭包并调用它的简写方法.与PHP 5.6的 bindTo 相比,它的性能要快得多.

示例 -  PHP 7之前

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

示例 -  PHP 7 +

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

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

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

它产生以下浏览器输出 :

1