5

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.

3
  • Are you looking for func_get_args() or for an alternative to it? Commented Mar 27, 2013 at 15:34
  • unfortunatelly - neither Commented Mar 27, 2013 at 15:40
  • Since you can't name parameters in PHP, how can a function understand if you're passing first 3 or last 3 arguments? Unless you use an array (but you said you want to avoid it) Commented Mar 27, 2013 at 15:49

8 Answers 8

5

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
    }

    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

+ Using arrays is common approach, found in most frameworks, for optional, named arguments. Also look at extract().
1

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

2 Comments

to check what kind of arguments there is i have to put all (four in this case) variables to function... i would sometimes put one, another time - 3 so thats not the solution
So you need to pass an array of parameters so you can use the array keys as labels which paramameters you have passed (x, y, m or l).
1

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

Comments

0

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

5 Comments

@JasonMcCreary No sorry, I didn't before you asked for it :).
While this does leverage the dynamic nature of PHP, there would be no way to differential between the arguments.
exactly - as jacon mccreary said - if i would write abc(1,2,4) i won't know that 4 is the $m instead of $l :(
You are right. The only other way that I know, is just like @JasonMcCreary described, with an array with key=>value pairs.
i've hoped that there is some kind of "magical" solution - brilliant, elegant, short and working... unfortunatelly life is life.. dirty, grey and long like a toilet paper :D
0
// 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]))
}

Comments

0

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

Comments

0
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

9 Comments

instead of null you can always send abc(0,0,1,0) and then check it - if $x = 0 then ignore it. i am aware of that solution from the beginnig, but surely it is not elegant way.
Why it's not elegant? What do you mean by "elegance"?
Yeah, but you wouldn't do "a lot of ifs" or use arrays.
consider it as a simplified example, but function for example might take 10 or 20 arguments. Now surely unelegant way is to call a function like this: abc(1, 0, 1, 1, 0, 0, 0, 1, 2, 5, 6, 7, 2, 0, 0, 2) Maybe it doesnt look bad but how can you remember whitch argument is which?
Then call by array: abc(array('x'=>'1', 'y'=>0)) or make a class (a bit overkill) for it and call it by: $abc = new Abc(); $abc->setX(1); $abc->setY(0); $abc->run();
|
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.