在 PHP 中将数组作为参数传递,而不是数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/744145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:43:25  来源:igfitidea点击:

Passing an Array as Arguments, not an Array, in PHP

phparraysfunctionmethods

提问by Robert K

I seem to remember that in PHP there is a way to pass an array as a list of arguments for a function, dereferencing the array into the standard func($arg1, $arg2)manner. But now I'm lost on how to do it. I recall the manner of passing by reference, how to "glob" incoming parameters ... but not how to de-list the array into a list of arguments.

我似乎记得在 PHP 中有一种方法可以将数组作为函数的参数列表传递,将数组解引用为标准func($arg1, $arg2)方式。但现在我不知道该怎么做。我记得通过引用传递的方式,如何“glob”传入参数......但不记得如何将数组从参数列表中删除。

It may be as simple as func(&$myArgs), but I'm pretty sure that isn't it. But, sadly, the php.net manual hasn't divulged anything so far. Not that I've had to use this particular feature for the last year or so.

它可能像 一样简单func(&$myArgs),但我很确定不是这样。但是,遗憾的是,php.net 手册到目前为止还没有透露任何内容。并不是说我在过去一年左右的时间里不得不使用这个特殊功能。

回答by simonhamp

As has been mentioned, as of PHP 5.6+ you can (should!) use the ...token (aka "splat operator", part of the variadic functionsfunctionality) to easily call a function with an array of arguments:

如前所述,从 PHP 5.6+ 开始,您可以(应该!)使用...令牌(又名“splat 运算符”,可变参数函数功能的一部分)轻松调用带有参数数组的函数:

<?php
function variadic($arg1, $arg2)
{
    // Do stuff
    echo $arg1.' '.$arg2;
}

$array = ['Hello', 'World'];

// 'Splat' the $array in the function call
variadic(...$array);

// 'Hello World'

Note: array items are mapped to arguments by theirpositionin the array, not their keys.

注意:数组项通过它们在数组中位置而不是它们的键映射到参数

As per CarlosCarucce's comment, this form of argument unpacking is the fastest method by farin all cases. In some comparisons, it's over 5x faster than call_user_func_array.

根据CarlosCarucce 的评论,这种形式的参数解包是迄今为止所有情况下最快的方法。在某些比较中,它比call_user_func_array.

Aside

在旁边

Because I think this is really useful (though not directly related to the question): you can type-hint the splat operator parameterin your function definition to make sure all of the passed values match a specific type.

因为我认为这真的很有用(尽管与问题没有直接关系):您可以在函数定义中对splat 运算符参数进行类型提示,以确保所有传递的值都匹配特定类型。

(Just remember that doing this it MUSTbe the lastparameter you define and that it bundles all parameters passed to the function into the array.)

(请记住,这样做必须是您定义的最后一个参数,并且它将传递给函数的所有参数捆绑到数组中。)

This is great for making sure an array contains items of a specific type:

这对于确保数组包含特定类型的项目非常有用:

<?php

// 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);

回答by Jeff Ober

Also note that if you want to apply an instance method to an array, you need to pass the function as:

另请注意,如果要将实例方法应用于数组,则需要将函数传递为:

call_user_func_array(array($instance, "MethodName"), $myArgs);

回答by DanMan

For sake of completeness, as of PHP 5.1 this works, too:

为了完整起见,从 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"
?>

See: http://php.net/reflectionfunction.invokeargs

请参阅:http: //php.net/reflectionfunction.invokeargs

For methods you use ReflectionMethod::invokeArgsinstead and pass the object as first parameter.

对于方法,您改用ReflectionMethod::invokeArgs并将对象作为第一个参数传递。