How to pass variable number of arguments to a PHP function

我有一个 PHP 函数,它接受变量数量的参数(使用 func_num_args()func_get_args()) ,但是我想传递函数的参数数量取决于数组的长度。有没有一种方法来 打电话一个 PHP 函数与一个可变数量的参数?

120700 次浏览

如果您的参数在数组中,那么您可能会对 call_user_func_array函数感兴趣。

如果要传递的参数数量取决于数组的长度,这可能意味着您可以将它们打包到数组中——并将其用于 call_user_func_array的第二个参数。

然后,您传递的数组的元素将作为不同的参数被函数接收。


例如,如果你有这个函数:

function test() {
var_dump(func_num_args());
var_dump(func_get_args());
}

您可以将参数打包成一个数组,如下所示:

$params = array(
10,
'glop',
'test',
);

And, then, call the function :

call_user_func_array('test', $params);

这段代码将输出:

int 3


array
0 => int 10
1 => string 'glop' (length=4)
2 => string 'test' (length=4)

也就是说,3个参数; 完全相同的函数是这样调用的:

test(10, 'glop', 'test');

你可以直接打电话。

function test(){
print_r(func_get_args());
}


test("blah");
test("blah","blah");

产出:

数组([0] = > 呐呐)数组([0] = > 呐呐[1] = > 呐呐)

对于那些想用 $object->method做到这一点的人:

call_user_func_array(array($object, 'method_name'), $array);

我成功地在一个构造函数中使用变量参数调用了变量 method _ name。

现在是 可以使用 PHP 5.6. x,使用... 运算符(在某些语言中也称为 splat 运算符) :

例如:

function addDateIntervalsToDateTime( DateTime $dt, DateInterval ...$intervals )
{
foreach ( $intervals as $interval ) {
$dt->add( $interval );
}
return $dt;
}


addDateIntervaslToDateTime( new DateTime, new DateInterval( 'P1D' ),
new DateInterval( 'P4D' ), new DateInterval( 'P10D' ) );

下面是使用魔法方法 _ _ 调用的解决方案

(可在 php 5.3下载)

class Foo {
public function __invoke($method=null, $args=[]){
if($method){
return call_user_func_array([$this, $method], $args);
}
return false;
}


public function methodName($arg1, $arg2, $arg3){


}
}

来自同一个班级:

$this('methodName', ['arg1', 'arg2', 'arg3']);

从对象的实例:

$obj = new Foo;
$obj('methodName', ['arg1', 'arg2', 'arg3'])

在新的 < a href = “ http://Php.net/archive/2014.Php # id2014-10-16-3”rel = “ noReferrer”> Php 5.6 中,您可以使用 ... operator而不是使用 func_get_args()

因此,使用这个函数,您可以获得所有传递的参数:

function manyVars(...$params) {
var_dump($params);
}

An old question, I know, however, none of the answers here really do a good job of simply answer the question.

我只是把玩了一下 php,解决方案是这样的:

function myFunction($requiredArgument, $optionalArgument = "default"){
echo $requiredArgument . $optionalArgument;
}

这个函数可以做两件事:

如果仅使用所需的参数 myFunction("Hi")调用它 It will print "Hi default"

但是如果使用可选参数 myFunction("Hi","me")调用它 它将打印“你好我”;

I hope this helps anyone who is looking for this down the road.

Since PHP 5.6, a variable argument list can be specified with the ... operator.

function do_something($first, ...$all_the_others)
{
var_dump($first);
var_dump($all_the_others);
}


do_something('this goes in first', 2, 3, 4, 5);


#> string(18) "this goes in first"
#>
#> array(4) {
#>   [0]=>
#>   int(2)
#>   [1]=>
#>   int(3)
#>   [2]=>
#>   int(4)
#>   [3]=>
#>   int(5)
#> }

如您所见,...操作符收集数组中的变量参数列表。

如果需要将变量参数传递给另一个函数,...仍然可以帮助您。

function do_something($first, ...$all_the_others)
{
do_something_else($first, ...$all_the_others);
// Which is translated to:
// do_something_else('this goes in first', 2, 3, 4, 5);
}

PHP 7以来,参数的变量列表也可以强制为同一类型的 所有

function do_something($first, int ...$all_the_others) { /**/ }

我很惊讶这里没有人提到简单地传递和 提取物ing 一个数组。例如:

function add($arr){
extract($arr, EXTR_REFS);
return $one+$two;
}
$one = 1;
$two = 2;
echo add(compact('one', 'two')); // 3

当然,这并不提供 论点确认。 为此,任何人都可以使用我期望的函数: < a href = “ https://gist.github.com/iauto/8063fc78e9508ed427d5”rel = “ nofollow”> https://gist.github.com/iautomation/8063fc78e9508ed427d5

我想知道,我找不到关于将 命名论点(自从 PHP8)与 变量参数结合使用的可能性的文档。因为我尝试了这段代码,我很惊讶,它居然真的起作用了:

function myFunc(...$args) {
foreach ($args as $key => $arg) {
echo "Key: $key Arg: $arg <br>";
}
}
echo myFunc(arg1:"foo", arg2:"bar");

产出:

Key: arg1 Arg: foo
Key: arg2 Arg: bar

在我看来,这很酷。