我有一个 PHP 函数,它接受可变数量的参数(使用 func_num_args()
和 func_get_args()
),但是我想要传递给函数的参数数量取决于一个数组的长度。有没有一种方法可以使用可变数量的参数 调用 PHP 函数?
我有一个 PHP 函数,它接受可变数量的参数(使用 func_num_args()
和 func_get_args()
),但是我想要传递给函数的参数数量取决于一个数组的长度。有没有一种方法可以使用可变数量的参数 调用 PHP 函数?
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',
);
接下来,调用该函数:
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 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' ) );
... operator
而不是使用func_get_args()
来获取传递的所有参数。
因此,使用它,您可以获取传递的所有参数:
function manyVars(...$params) {
var_dump($params);
}
自从PHP 5.6起,可以使用...
运算符指定变量参数列表。
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) { /**/ }
如果您正在寻找一种使用$object->method
来完成此操作的方法:
call_user_func_array(array($object, 'method_name'), $array);
我在一个构造函数中成功地使用了这个方法,该方法调用了一个带有变量参数的变量 method_name。
$object->method_name(...$array);
。 - Yoann Kergall$object->method_name(&...$args);
。 - Fabien Haddadifunction test(){
print_r(func_get_args());
}
test("blah");
test("blah","blah");
输出:
数组([0] => blah) 数组([0] => blah [1] => blah)
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
function add($arr){
extract($arr, EXTR_REFS);
return $one+$two;
}
$one = 1;
$two = 2;
echo add(compact('one', 'two')); // 3
extract
不应该被使用... 使用它是一个非常糟糕的想法。 - PapsOu这是一个老问题,我知道,然而,这里的任何答案都没有很好地回答这个问题。
我刚刚尝试了一下 PHP,解决方案看起来像这样:
function myFunction($requiredArgument, $optionalArgument = "default"){
echo $requiredArgument . $optionalArgument;
}
这个函数可以做两件事情:
如果只使用必需的参数调用它:myFunction("Hi")
它将打印出 "Hi default"
但是,如果使用可选参数调用它:myFunction("Hi","me")
它将打印出 "Hi me";
我希望这能帮助那些在未来需要这个的人。
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'])
call_user_func_array
,正如2009年最受欢迎的答案所述。也许在它周围包装一个额外的__invoke
有点意义,但我看不出来。 - a20