View Full Version : Can I pass a function name to a function and then call that function?

12-08-2008, 05:06 PM
I'm not where I can try this, otherwise I would just try it. Can you send a function name as an argument to a function, then call that function from within the called function? For example:

myFunction ("calculateSomething");

function myFunction ($funcName) }

$a = $funcName();

echo $a; //should be 2

function calculateSomething() {

return 1 + 1;


12-08-2008, 05:08 PM
That works just fine.


12-08-2008, 11:58 PM
Callback's baby ;D, the closest thing PHP gets to function pointers.

I'd actually recommend using the execution function with call_user_func or call_user_func_array. I suggest this for situations where you'll be using an object callback, which pre-5.3 I believe it is it doesn't tend to like the Paamayim Nekudotayim accessor for class methods for variable functions (as in, a function that is a variable).
Now, let hope that zend promotes callback to an object so we can at least typecast the function to require one.

Callback link (which is tough to find): http://us3.php.net/manual/en/language.pseudo-types.php#language.types.callback

12-09-2008, 12:21 AM
Hey, that's what I want, a callback. Awesome.