提出
在匿名函式出現之前,所有的函式都需要先命名才能使用
1 function increment($value) 2 { 3 return $value + 1; 4 } 5 6 array_map('increment', [1, 2, 3]);
有的時候函式可能只需要使用一次,這時候使用匿名函式會使得代碼更加簡潔直觀,同時也避免了函式在其他地方被使用
1 array_map(function($value){ 2 return $value + 1; 3 }, [1, 2, 3]);
定義和使用
PHP 將閉包和匿名函式視為同等概念(本文統稱為匿名函式),本質上都是偽裝成函式的物件,
匿名函式的本質是物件,因此跟物件一樣可將匿名函式賦值給某一變數
1 $greet = function(string $name){ 2 echo "hello {$name}"; 3 } 4 5 $greet("jack") // hello jack
所有的匿名函式都是 Closure 物件的實體
$greet instanceof Closure // true
物件并沒有什么父作用域可言,所以需要使用 use 來手動宣告使用的變數,
1 $num = 1; 2 $func = function() use($num){ 3 $num = $num + 1; 4 echo $num; 5 } 6 $func(); // 2 7 echo $num; // 還是 1
如果要讓匿名函式中的變數生效,需要使用參考傳值
1 $num = 1; 2 $func = function() use(&$num){ 3 $num = $num + 1; 4 echo $num; 5 } 6 $func(); // 2 7 echo $num; // 2
從 PHP 5.4 開始,在類里面使用匿名函式時,匿名函式的 $this 將自動系結到當前類
1 class Foo { 2 public function bar() 3 { 4 return function() { 5 return $this; 6 }; 7 } 8 } 9 10 $foo = new Foo(); 11 $obj = $foo->bar(); // Closure() 12 $obj(); // Foo
如果不想讓自動系結生效,可使用靜態匿名函式
1 class Foo { 2 public function bar() 3 { 4 return static function() { 5 return $this; 6 }; 7 } 8 } 9 $foo = new Foo(); 10 $obj = $foo->bar(); // Closure() 11 $obj(); // Using $this when not in object context
匿名函式的本質
匿名函式的本質是 Closure 物件,包括了以下五個方法
1 Closure { 2 private __construct ( void ) 3 public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure 4 public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure 5 public call ( object $newthis [, mixed $... ] ) : mixed 6 public static fromCallable ( callable $callable ) : Closure
}
__construct - 防止匿名函式被實體化
$closure = new \Closure(); // PHP Error: Instantiation of 'Closure' is not allowed
Closure::bindTo - 復制當前匿名函式物件,系結指定的 $this 物件和類作用域,通俗的說,就是手動將匿名函式與指定物件系結,利用這點,可以擴展物件的功能,
1 // 定義商品類 2 class Good { 3 private $price; 4 5 public function __construct(float $price) 6 { 7 $this->price = $price; 8 } 9 } 10 11 // 定義一個匿名函式,計算商品的促銷價 12 $addDiscount = function(float $discount = 0.8){ 13 return $this->price * $discount; 14 } 15 16 $good = new Good(100); 17 18 // 將匿名函式系結到 $good 實體,同時指定作用域為 Good 19 $count = $addDiscount->bindTo($good, Good::class); 20 $count(); // 80 21 22 // 將匿名函式系結到 $good 實體,但是不指定作用域,將無法訪問 $good 的私有屬性 23 $count = $addDiscount->bindTo($good); 24 $count(); // 報錯
Closure::bind - bindTo 方法的靜態版本,有兩種用法:
用法一:實作與 bindTo 方法同樣的效果
$count = \Closure::bind($addDiscount, $good, Good::class);
用法二:將匿名函式與類(而不是物件)系結,記得要將第二個引數設定為 null
1 // 商品庫存為 10 2 class Good { 3 static $num = 10; 4 } 5 6 // 每次銷售后回傳當前庫存 7 $sell = static function() { 8 return"當前庫存為". --static::$num ; 9 }; 10 11 // 將靜態匿名函式系結到 Good 類中 12 $sold = \Closure::bind($sell, null, Good::class); 13 14 $sold(); // 當前庫存為 9 15 $sold(); // 當前庫存為 8
call - PHP 7 新增的 call 方法可以實作系結并呼叫匿名函式,除了語法更加簡潔外,性能也更高
1 // call 版本 2 $addDiscount->call($good, 0.5); // 系結并傳入引數 0.5,結果為 50 3 4 // bindTo 版本 5 $count = $addDiscount->bindTo($good, Good::class); 6 $count(0.5); // 50
fromCallable - 將給定的 callable 函式轉化成匿名函式
1 class Good { 2 private $price; 3 4 public function __construct(float $price) 5 { 6 $this->price = $price; 7 } 8 } 9 10 function addDiscount(float $discount = 0.8){ 11 return $this->price * $discount; 12 } 13 14 $closure = \Closure::fromCallable('addDiscount'); 15 $good = new Good(100); 16 $count = $closure->bindTo($good); 17 $count = $closure->bindTo($good, Good::class); // 報錯,不能重復系結作用域 18 $count(); // 報錯,無法訪問私有屬性
fromCallable 等價于
1 $reflexion = new ReflectionFunction('addDiscount'); 2 $closure = $reflexion->getClosure();
這里有一點需要特別注意的是,無論是 fromCallable 轉化成的閉包,還是使用反射得到的閉包,在使用 bindTo時,如果第二個引數指定系結類,會報錯
Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/115607.html
標籤:PHP
上一篇:[Php] windows下使用composer出現SHA384 is not supported by your openssl extension
下一篇:AJAX回傳資料型別
