【问题标题】:Passing an Array as Arguments, not an Array, in PHP在 PHP 中将数组作为参数而不是数组传递
【发布时间】:2026-01-18 00:00:02
【问题描述】:

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

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

【问题讨论】:

    标签: php arrays function methods


    【解决方案1】:

    【讨论】:

    • 在 PHP 5.6 (wiki.php.net/rfc/argument_unpacking) 中将添加所谓的参数解包。它将使用“splat 运算符”:“...”。其语法:$args = [1,2,3]; function(...$args){}
    • 但是如果你想调用的函数是一个对象的实例方法,而不是全局空间中的函数名呢?
    • @ahnbizcad 那么你应该使用callable,它使用相同的call_user_func_array,只有第一个元素是对象的数组,第二个是方法。例如call_user_func_array([$object, 'method'], $myArgs);
    【解决方案2】:

    As has been mentioned,从 PHP 5.6+ 开始,您可以(应该!)使用... 标记(又名“splat 运算符”,variadic functions 功能的一部分)轻松调用带有参数数组的函数:

    <?php
    function variadic($arg1, $arg2)
    {
        // Do stuff
        echo $arg1.' '.$arg2;
    }
    
    $array = ['Hello', 'World'];
    
    // 'Splat' the $array in the function call
    variadic(...$array);
    
    // 'Hello World'
    

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

    根据CarlosCarucce's comment,这种形式的参数解包是迄今为止最快的方法。在某些比较中,它比 call_user_func_array 快 5 倍以上。

    一边

    因为我认为这非常有用(尽管与问题没有直接关系):您可以在函数定义中type-hint the splat operator parameter 以确保所有传递的值都与特定类型匹配。

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

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

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

    【讨论】:

    • 这比call_user_func_array 有很大的性能提升。因此,如果您使用的是 php 5.6+,我会推荐这个。 Here is a test,引用于official php wiki
    【解决方案3】:

    还要注意,如果要对数组应用实例方法,需要将函数传递为:

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

    【讨论】:

    • @understack linked page 上的$foo-&gt;bar() 示例表明它应该是array($instance, "MethodName")
    • 太棒了,我用它来避免在子类中重复构造函数参数:) call_user_func_array(array(parent, "__construct"), func_get_args());
    【解决方案4】:

    为了完整起见,从 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 的方法,并将对象作为第一个参数传递。

    【讨论】: