## 匿名函數
[匿名函數](http://php.net/manual/zh/functions.anonymous.php)(Anonymous functions),也叫[閉包函數](http://php.net/manual/zh/class.closure.php)(closures),允許 臨時創建一個沒有指定名稱的函數。最經常用作回調函數(callback)參數的值。當然,也有其它應用的情況。
匿名函數目前是通過 Closure 類來實現的。
~~~
// 定義一個閉包,并把它賦給變量 $f
$f = function () {
return 7;
}
// 使用閉包也很簡單
$f(); //這樣就調用了閉包,輸出 7
// 當然更多的時候是把閉包作為參數(回調函數)傳遞給函數
function testClosure (Closure $callback) {
return $callback();
}
// $f 作為參數傳遞給函數 testClosure,如果是普遍函數是沒有辦法作為testClosure的參數的
testClosure($f);
// 也可以直接將定義的閉包作為參數傳遞,而不用提前賦給變量
testClosure (function () {
return 7;
});
// 閉包不止可以做函數的參數,也可以作為函數的返回值
function getClosure () {
return function () { return 7; };
}
$c = getClosure(); // 函數返回的閉包就復制給 $c 了
$c(); // 調用閉包,返回 7
~~~
閉包類:https://laravel-china.org/articles/4625/php-closure-closure