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