$arr = array($param1, $param2);
call_user_func(array($object, $function), ...);
I need to create a list of params from $arr values and put it instead of …, so the result would be:
call_user_func(array($object, $function), $param1, $param2);
I don’t want to do it like this:
call_user_func(array($object, $function), $arr);
Size of $arr array can change, it can have 3, 4 … or more valus.
Any ideas?
call_user_func_array works very well, thanks
sutabi
3
Use call_user_func_array:
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\
";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\
";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>
You can also get the dynamic list of parameters via func_num_args:
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />\
";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />\
";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br />\
";
}
}
foo(1, 2, 3);
?>
Well that is not what I need to achieve.
I need to convert array into list of variables.
Another example:
$arr = array($p1, $p2, $p3);
function test($p1, $p2, $p3){
}
test(convert_to_list_of_variables($arr));
convert_to_list_of_variables($arr) should create something like $p1, $p2, $p3, so the result would be test($p1, $p2, $p3)
oddz
5
call_user_func_array(‘test’,$arr);