編程學習網 > 編程教程 > PHP7 教程
2016
09-20

PHP7 Closure::call()

清華大佬耗費三個月吐血整理的幾百G的資源,免費分享!....>>>

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

掃碼二維碼 獲取免費視頻學習資料

編程學習