Kalreg
Kalreg

Reputation: 941

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:

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.

Upvotes: 5

Views: 4507

Answers (8)

Tushar Nain
Tushar Nain

Reputation: 1

function func1(...$params){
    
}

function func2(...$params){
    func1(...$params);
}


func2($x, $y, $z);

This way is working as expected, you can even pass parameters by reference.

Upvotes: 0

Thomas Laier Pedersen
Thomas Laier Pedersen

Reputation: 449

function abc ($x=null, $y=null, $l=null, $m=null) {

}

You would then call the function like:

all ($x, $y, $l, $m) arguments

first three ($x, $y, $l) arguments

first and third ($x, null, $l) arguments

only second (null, $y) argument

last two (null, null, $l, $m) arguments

Upvotes: 0

Leandro Arruda
Leandro Arruda

Reputation: 506

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.

$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.

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

Upvotes: 1

alwaysLearn
alwaysLearn

Reputation: 6950

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)

or what is was thinking of is something using call_user_func_array like

 $params = array('','y','','m');
 call_user_func_array('abc', $params);

Upvotes: 0

Akshay Deep Giri
Akshay Deep Giri

Reputation: 3280

// 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]))
}

Upvotes: 0

Benz
Benz

Reputation: 2335

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

Upvotes: 0

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

Have a look at func_get_args(), func_get_arg() and 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). You have to call abc($x, null, $l).

Upvotes: 1

Alvaro
Alvaro

Reputation: 41595

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
    }

    ...
}

Upvotes: 5

Related Questions