Passing dynamic parameters to a dynamic function call
Posted: Mon Jul 07, 2008 7:11 pm
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:
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:
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.
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(??????????);
}
Code: Select all
public function getNames($ageGreaterThanEqualTo=0, $ageLessThanEqualTo=0)
{
...
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.