maaudet
maaudet

Reputation: 2358

Using closures in an array

I have a code similar to this:

$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;

The error I get is:

Function name must be a string in xxxx

Is there any ways I could use an array to store multiple closures and is there a working way to access them?

I use PHP 5.4.3

Upvotes: 3

Views: 5832

Answers (2)

desimusxvii
desimusxvii

Reputation: 1094

This works fine for me in php 5.3.10.

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

Upvotes: 4

Thomas
Thomas

Reputation: 1401

Yeah, use call_user_func instead.

Upvotes: 4

Related Questions