简体   繁体   English

php - 传递给函数的动态变量数量

[英]php - dynamic amount of variables passing to function

I am looking for the most elegant way to pass variable number of arguments to pass a function.我正在寻找最优雅的方式来传递可变数量的参数来传递函数。

Consider such a function:考虑这样一个函数:

function abc ($x, $y, $l, $m) {
...
}

Is there an elegant way without lots of "ifs" or array of variables pasing as one variable to call a function with:有没有一种优雅的方式,没有很多“ifs”或变量数组作为一个变量来调用函数:

all ($x, $y, $l, $m) arguments
first three ($x, $y, $l) arguments
first and third ($x, $l) arguments
only second ($y) argument
last two ($l, $m) arguments

Please advice.请指教。

I would rather use an array as a parameter and inside the function check it to see if your variable exists:我宁愿使用数组作为参数并在函数内部检查它以查看您的变量是否存在:

function abc ($data) {

    if(isset($data['x']){
       //whatever
    }

    if(isset($data['y']){
       //whatever
    }

    ...
}

Have a look at func_get_args() , func_get_arg() and func_num_args() .看看func_get_args()func_get_arg()func_num_args()

Of course when you use these there is no way you can get "first" and "third" argument only by calling abc($x, $l) .当然,当您使用这些时,您无法仅通过调用abc($x, $l)来获得“第一个”和“第三个”参数。 You have to call abc($x, null, $l) .你必须调用abc($x, null, $l)

Are you trying to say that you want to check how many variables are set in the function?你是想说你想检查函数中设置了多少个变量? Maybe this can help也许这可以帮助

function abc($a) {
    $args = func_num_args();
    for($i = 0; $i < $args; $i++) {
        print_r(func_get_arg($i));   
    }
}
abc(1,2,3,4);
// use **func_get_args** function to grab all arg passed in funcion
function variadic($a,$b) {
    $args = func_get_args();
    foreach ($args as $arg) {
        echo "$arg<br />\n";
    }
}

// pass as array 
$arg = array('a'=> $a,'a'=> $b)
function variadic($arg) 
{
   if(isset($arg[a]))
}

I cant think of any exact alternative to your solution ... either you have to pass null to the parameters you dont want to send like abc (null, $y, null, $m)我想不出您的解决方案的任何确切替代方案......要么您必须将 null 传递给您不想发送的参数,如abc (null, $y, null, $m)

or what is was thinking of is something using call_user_func_array like或者想到的是使用 call_user_func_array 之类的东西

 $params = array('','y','','m');
 call_user_func_array('abc', $params);
function abc ($x=null, $y=null, $l=null, $m=null) {

}

You would then call the function like:然后,您将调用该函数,如:

all ($x, $y, $l, $m) arguments所有 ($x, $y, $l, $m) 参数

first three ($x, $y, $l) arguments前三个 ($x, $y, $l) 参数

first and third ($x, null, $l) arguments第一个和第三个 ($x, null, $l) 参数

only second (null, $y) argument只有第二个 (null, $y) 参数

last two (null, null, $l, $m) arguments最后两个 (null, null, $l, $m) 参数

I recently needed to have a function to call others functions dynamically and was try to make something most elegant and easy to maintain.我最近需要一个函数来动态调用其他函数,并试图制作最优雅和易于维护的东西。 Maybe this approach can be useful for who instead need to pass different number of args to the same function look for how to use one way to call any function desired.也许这种方法对于需要将不同数量的 args 传递给同一个函数的人有用,以寻找如何使用一种方法来调用所需的任何函数。

$return = call_user_func_array($function_name, $parameters);

In my case I pass a function available in a web service and feed $parameters array with the argument required by each function.在我的例子中,我传递了一个 Web 服务中可用的函数,并将每个函数所需的$parameters提供给$parameters数组。

https://secure.php.net/manual/en/function.call-user-func-array.php https://secure.php.net/manual/en/function.call-user-func-array.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM