Reputation: 9431
In short, what I want is a kind of export() function (but not export()), it creates new variables in a symbols table and returns the number of created vars.
I'm trying to figure out if it is possible to declare a function
function foo($bar, $baz)
{
var_dump(func_get_args());
}
And after that pass an array so that each value of array would represent parameters.
Just wondering if it is possible (seems that is not).
I need this for dynamic loading, so number of arguments, size of array may vary - please don't offer to pass it as
foo($arr['bar']);
and so on.
Again, ideal thing solution will look like
foo(array('foo'=>'1', 'bar'=>'2', ..., 'zzz'=>64));
for declaration
function foo($foo, $bar, ..., $zzz) {}
As far as I rememeber in some dynamic languages lists may behave like that (or maybe I'm wrong).
(I want to create dynamically parameterized methods in a class and enjoy a built-in mechanism of controlling functions' number of arguments, default value, and so on. Such a mechanism would allow me to get rid of array params and func_get_args()
and func_get_num()
calls in the method body).
Upvotes: 1
Views: 226
Reputation: 1996
I don't know about the speed but you could use ReflectionFunction::getParameters() to get what name you gave the parameters, combined with call_user_func_array() to call the function. ReflectionFunction::invokeArgs() could be used as well for invoking.
Upvotes: 2
Reputation: 44093
How about using extract()?
<?php
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
(Code taken from PHP example)
Upvotes: 0
Reputation: 13427
You're looking for call_user_func_array
example:
function foo($bar, $baz)
{
return call_user_func_array('beepboop',func_get_args());
}
function beepboop($bar, $baz){
print($bar.' '.$baz);
}
foo('this','works');
//outputs: this works
Upvotes: 4