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

明確的學習思路能更高效的學習

點此加入該群學習
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/119129.html
標籤:PHP
上一篇:Centos下安裝PHP ldap擴展
下一篇:正確理解 PHP 的多載