3

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

2
  • 1
    It seems a little odd to me that you're calling the function by reference. When testing I have this warning: Strict Standards: Only variables should be assigned by reference Commented Sep 6, 2012 at 19:34
  • Yes, I removed the return by reference, editing post. Commented Sep 6, 2012 at 19:35

2 Answers 2

4

This works fine for me in php 5.3.10.

$m = array();
$m['fish'] = function() { print "Hello\n"; };
$f = $m['fish'];
$f();
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, on the actual code I had made a typo, that's why that it didn't work. Thanks for the answer!
4

Yeah, use call_user_func instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.