$closureFunc = function(){
return new RedisDB([
'host' => '127.0.0.1',
'port' => 6379
]);
};
$closureFunc();複製代碼
上面代碼是一個簡單的匿名函數寫法php
print_r($closureFunc);
$instance = call_user_func($closureFunc);
print_r($instance);複製代碼
如今打印如上語句,其結果同樣的,都是RedisDB對象:html
app\components\RedisDB Object
(
[_di:protected] =>
[_options:protected] => Array
(
[host] => 127.0.0.1
[port] => 6379
)
)
app\components\RedisDB Object
(
[_di:protected] =>
[_options:protected] => Array
(
[host] => 127.0.0.1
[port] => 6379
)
)複製代碼
若是打印print_r($closureFunc);其結果是Closure Object:web
Closure Object
(
[this] => app\modules\student\controllers\IndexController Object
(
[modelClass] => app\models\db\XmUsers
[response] => app\common\base\BaseResponseService Object
(
[format] => json
[acceptMimeType] => application/json
[acceptParams] => Array
(
)
[formatters] => Array
(
[html] => Array
(
[class] => yii\web\HtmlResponseFormatter
)
[xml] => Array
(
[class] => yii\web\XmlResponseFormatter
)
[json] => Array
(
[class] => yii\web\JsonResponseFormatter
)
[jsonp] => Array
(
[class] => yii\web\JsonResponseFormatter
[useJsonp] => 1
)
) 複製代碼
上面結果我只截取一小部分,確實是Closure對象json
也就是說call_user_func參數能夠是匿名對象,官方文檔有這麼一句話:bash
call_user_func may also be used to call a closure or anonymous function that has been passed into a user-defined function.複製代碼
大意是:call_user_func還可用於調用已傳遞到用戶定義函數的閉包或匿名函數。閉包
參考:www.php.cn/php-weiziji…app