3

私はこれに似たコードを持っています:

$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を使用しています

4

2 に答える 2

4

ええ、代わりに call_user_func を使用してください。

于 2012-09-06T19:30:31.397 に答える
4

これはphp 5.3.10でうまくいきます。

$m = array();
$m['fish'] = function() { print "Hello\n"; };
$f = $m['fish'];
$f();
于 2012-09-06T19:33:03.670 に答える