代码之家  ›  专栏  ›  技术社区  ›  nohat

如何将变量数的参数传递给PHP函数

  •  107
  • nohat  · 技术社区  · 15 年前

    我有一个PHP函数,它接受可变数量的参数(使用 func_num_args() func_get_args() ,但要传递函数的参数数目取决于数组的长度。有办法去吗 呼叫 参数数目可变的PHP函数?

    9 回复  |  直到 8 年前
        1
  •  121
  •   Pascal MARTIN    15 年前

    如果在数组中有参数,则可能对 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个参数;与iof函数的调用方式完全相同:

    test(10, 'glop', 'test');
    
        2
  •  52
  •   jonathancardoso Jani Hartikainen    11 年前

    这是现在 possible with 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' ) );
    
        3
  •  42
  •   Salvador Dali    10 年前

    在新的 Php 5.6 ,你可以使用 ... operator 而不是使用 func_get_args() .

    所以,使用这个,你可以得到你传递的所有参数:

    function manyVars(...$params) {
       var_dump($params);
    }
    
        4
  •  35
  •   Daniele Orlando    9 年前

    自从 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) { /**/ }
    
        5
  •  10
  •   rink.attendant.6    10 年前

    对于那些想用什么方法来做这件事的人 $object->method :

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

    我在一个用变量参数调用变量方法名的构造函数中成功地使用了这个方法。

        6
  •  4
  •   Donnie C    15 年前

    你可以叫它。

    function test(){        
         print_r(func_get_args());
    }
    
    test("blah");
    test("blah","blah");
    

    输出:

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

        7
  •  2
  •   iautomation    8 年前

    我很惊讶这里没有人提到过路过 extract 一个数组。例如:

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

    当然,这不提供 argument validation . 为此,任何人都可以使用我的Expect函数: https://gist.github.com/iautomation/8063fc78e9508ed427d5

        8
  •  1
  •   Alex    10 年前

    不过,我知道,一个老问题,这里的答案都不能很好地回答这个问题。

    我刚玩过PHP,解决方案如下:

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

    这个函数可以做两件事:

    如果仅使用所需参数调用它: myFunction("Hi") 它将打印“hi-default”

    但如果使用可选参数调用它: myFunction("Hi","me") 它会打印“嗨我”;

    我希望这能帮助任何在路上寻找这个的人。

        9
  •  0
  •   Dieter Gribnitz    10 年前

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

    (从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'])