$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