...

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



Fumigator
12-08-2008, 04: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;

}

eak
12-08-2008, 04:08 PM
Yes.
That works just fine.

http://us3.php.net/manual/en/functions.variable-functions.php

Fou-Lu
12-08-2008, 10: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

Fumigator
12-08-2008, 11:21 PM
Hey, that's what I want, a callback. Awesome.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum