Passing dynamic parameters to a dynamic function call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RR_QQ2
Forum Newbie
Posts: 4
Joined: Mon Jul 07, 2008 6:43 pm

Passing dynamic parameters to a dynamic function call

Post by RR_QQ2 »

Ok so I'm building a interface class I have serveral functions that I want to be able to call dynamically in the fashion shown:

Code: Select all

 
// initial function call
getMethod('getNames', array(23, 33));
 
public function getMethod($methodName=null, $methodParams=array())
{
      // dynamically call the function and pass the parameters
      return $members->$methodName(??????????);
}
 
Somewhere in my code I have defined a $members object of type Members. This class has a function 'getNames' that gets all names of members that have an age in between the two numbers passed as parameters to the function:

Code: Select all

 
public function getNames($ageGreaterThanEqualTo=0, $ageLessThanEqualTo=0)
{
    ...
 
As you can see I need to be able to assign in method "getMethod" the right number of parameters to pass the dynamic funcion call -> return $members->$methodName(??????????);

Again, the number of parameters in getMethod('getNames', array(23, 33)); (the second parameter array) can vary...so that means I can potentially have something like this also: getMethod('getFemales', array(23, 33, "mary")); (public function getFemales($ageFrom, $ageTo, $firstName)) or getMethod('getMales', array(23, 33, "Jay", "1930")); (public function getMales($ageFrom, $ageTo, $firstName, $yearBorn)). I need to be able to get it to work with any number of parameters depending on the function parameter definition.

So what I need is a way to get the $methodParams and somehow make it so that this line return $members->$methodName(??????????); will end up looking like this dynamically return $members->$methodName($methoParams[0], [1], ... , $methodParams[n-1]);

Is this possible to do without hardcoding? Thank you.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Passing dynamic parameters to a dynamic function call

Post by Eran »

What you are looking for is call_user_method_array() - http://www.php.net/manual/en/function.c ... -array.php
RR_QQ2
Forum Newbie
Posts: 4
Joined: Mon Jul 07, 2008 6:43 pm

Re: Passing dynamic parameters to a dynamic function call

Post by RR_QQ2 »

Ok....I see however if you see what I'm trying to do Im trying to call a function that belongs to a class -->> $members->$method(......);

So the call_user PHP function doesn't let me do this:

Code: Select all

 
call_user_func_array($members->$method, $methodParams);
 
Let me add that $members is a preexsting class...therefore i cant create a newInstance with a Reflector class.....

help!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Passing dynamic parameters to a dynamic function call

Post by Eran »

Check the syntax in the link I provided, I suggested a different function than what you used. You pass the method name as the first param, the object as the second and method parameters as the third

Code: Select all

 
call_user_method_array($method,$members,$methodParams);
 
RR_QQ2
Forum Newbie
Posts: 4
Joined: Mon Jul 07, 2008 6:43 pm

Re: Passing dynamic parameters to a dynamic function call

Post by RR_QQ2 »

Ok I did notice that you suggested a different function...the reason I wanted to use the other one instead was because call_user_method_array has been deprecated and I get this:

Strict Standards: Function call_user_method_array() is deprecated

However it does work! Thank you for your help. How do I address that 'is deprecated' warning? Thanks.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Passing dynamic parameters to a dynamic function call

Post by Eran »

You're right, my bad. Hadn't used it in a long time I guess 8O

The PHP manual suggests an alternative syntax to call_user_func_array to replace that deprecated method, by passing the object and method as array in the first parameter:

Code: Select all

 
call_user_func_array(array($members,$method),$methodParams);
 
(They mention this syntax for call_user_func, I hope it applies to call_user_func_array as well)
RR_QQ2
Forum Newbie
Posts: 4
Joined: Mon Jul 07, 2008 6:43 pm

Re: Passing dynamic parameters to a dynamic function call

Post by RR_QQ2 »

Nevermind..I wasn't reading the documentation right. I got it sorry and thank you for your patience. To use call_user_func_array I do this:

call_user_func_array(array($members, $method), $methodParams);

Again, thanks.

OOPS I see you replied before I could haha...thanks!
Post Reply