我记得在PHP中有一种方法可以将数组作为函数的参数列表传递,将数组解除引用成为标准的func($arg1, $arg2)
方式。但现在我不知道该怎么做了。我记得通过引用传递的方式,如何“全局”传递参数...但不知道如何将数组解除列表成为参数。
可能只需要像func(&$myArgs)
这样简单,但我很确定那不是正确的方法。但是,遗憾的是,php.net手册到目前为止还没有透露任何信息。虽然我过去一年左右没有使用过这个特定的功能。
我记得在PHP中有一种方法可以将数组作为函数的参数列表传递,将数组解除引用成为标准的func($arg1, $arg2)
方式。但现在我不知道该怎么做了。我记得通过引用传递的方式,如何“全局”传递参数...但不知道如何将数组解除列表成为参数。
可能只需要像func(&$myArgs)
这样简单,但我很确定那不是正确的方法。但是,遗憾的是,php.net手册到目前为止还没有透露任何信息。虽然我过去一年左右没有使用过这个特定的功能。
正如已经提到的那样, 在 PHP 5.6+ 中,您可以(应该!)使用 ...
标记(又称“扩展运算符”,是 变长函数 功能的一部分)来轻松调用带有参数数组的函数:
function variadic($arg1, $arg2)
{
// Do stuff
echo $arg1.' '.$arg2;
}
$array = ['Hello', 'World'];
// 'Splat' the $array in the function call
variadic(...$array);
// => 'Hello World'
$array = [
'arg2' => 'Hello',
'arg1' => 'World'
];
variadic(...$array);
// => 'World Hello'
(感谢mickmackusa提供此注释!)
根据CarlosCarucce的评论,这种参数解包形式在所有情况下都是最快的方法。在某些比较中,它比call_user_func_array
快5倍以上。
因为我认为这非常有用(虽然与问题没有直接关系):您可以在函数定义中类型提示splat运算符参数,以确保所有传递的值都匹配特定类型。
(只需记住,这样做必须是您定义的最后一个参数,并且它将所有传递给函数的参数捆绑到数组中。)
这非常适合确保数组包含特定类型的项目:
// Define the function...
function variadic($var, SomeClass ...$items)
{
// $items will be an array of objects of type `SomeClass`
}
// Then you can call...
variadic('Hello', new SomeClass, new SomeClass);
// or even splat both ways
$items = [
new SomeClass,
new SomeClass,
];
variadic('Hello', ...$items);
注意:此解决方案已过时,请参考simonhamp的答案获取更新的信息。
http://www.php.net/manual/en/function.call-user-func-array.php
call_user_func_array('func',$myArgs);
$args = [1,2,3];
function(...$args){}
- arothuiscall_user_func_array
,只不过第一个参数是一个数组,其中第一个元素是对象,第二个元素是方法名。例如:call_user_func_array([$object, 'method'], $myArgs);
- SteveB还要注意,如果您想将实例方法应用于数组,需要将函数传递为:
call_user_func_array(array($instance, "MethodName"), $myArgs);
$foo->bar()
зӨәдҫӢе»әи®®дҪҝз”Ёarray($instance, "MethodName")
гҖӮ - Paul Calcraftcall_user_func_array(array(parent, "__construct"), func_get_args());
- Jason为了完整起见,从PHP 5.1开始这也可以工作:
<?php
function title($title, $name) {
return sprintf("%s. %s\r\n", $title, $name);
}
$function = new ReflectionFunction('title');
$myArray = array('Dr', 'Phil');
echo $function->invokeArgs($myArray); // prints "Dr. Phil"
?>
查看:http://php.net/reflectionfunction.invokeargs
对于您使用的方法,请改用ReflectionMethod::invokeArgs,并将对象作为第一个参数传递。
call_user_func_array
相比,这种方法在性能上有很大的提升。因此,如果你使用的是php 5.6+的版本,我建议你使用这种方法。这里有一个测试,该测试被引用在官方php维基中。 - CarlosCarucce