在数组中使用闭包


Using closures in an array

我有一个类似的代码:

$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

有没有任何方法可以使用数组来存储多个闭包?有没有一种有效的方法来访问它们?

我使用PHP 5.4.3

这在php5.3.10中对我来说很好。

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

是的,请改用call_user_func。