Ok, this is a bit weird... we know this trick for variable functions:
$args = 'Hello';
$function_name = 'strtoupper';
print $function_name($args);
// executes 'strtoupper' and prints 'HELLO'
But what if you don’t know how many arguments you’re gonna have? And you know some functions require different numbers of inputs?
$args = array('1','10');
$function_name = 'rand';
print $function_name($args); //<--- Warning: rand() expects exactly 2 parameters, 1 given
So here’s what I’ve come up with... I also did this same type of thing using the
extract() function, but ... yuck.
$args = array('1','10');
$i = 'a';
foreach ($args as $k => $v) {
$$i = $v;
$i++;
}
$function_name = 'rand';
print $function_name($a,$b);
In reality, this solution is probably valid because functions usually only take 1 to 4 inputs... but can anyone thing of a solution that would be infinitely scalable? I can’t. It’s like I need a super-variant of the
list() function. I want to avoid
eval(), but I think this is a spot where it could be deployed.