Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组中使用闭包_Php - Fatal编程技术网

Php 在数组中使用闭包

Php 在数组中使用闭包,php,Php,我有一个类似的代码: $name = '_DBR'; // This comes from a function call $this->_Inits['_DBR'] = function () { return get_library('Database')->getConnection('users', 'read'); }; $inits = $this->_Inits[$name]; $result = $inits(); // ERROR: Function na

我有一个类似的代码:

$name = '_DBR'; // This comes from a function call

$this->_Inits['_DBR'] = function () { return get_library('Database')->getConnection('users', 'read'); };

$inits = $this->_Inits[$name];
$result = $inits(); // ERROR: Function name must be a string

return $result;
我得到的错误是:

Function name must be a string in xxxx
是否有任何方法可以使用数组存储多个闭包?是否有有效的方法访问它们


我使用PHP5.4.3

是的,改用call\u user\u func

这在PHP5.3.10中对我来说很好

$m = array();
$m['fish'] = function() { print "Hello\n"; };
$f = $m['fish'];
$f();

我觉得你通过引用调用函数有点奇怪。在测试时,我有这样一个警告:
严格标准:只有变量应该通过引用分配
是的,我删除了引用返回,编辑帖子。哇,在实际代码中,我输入了一个拼写错误,这就是它不起作用的原因。谢谢你的回答!