1

I'm writing several functions that pass arguments into the function, but many of them require 20-30 arguments to be passed to the function. For the purposes of this example, i'm only including the relevant portions of code in the function below.

For example, I want to pass arguments into a function like this:

function shortexample($id, $name, $pass) {
    $body = array(
        'username' => "$name",
        'password' => "$pass",
        'campaign_id' => $id
    );
}

Where I anticipate problems is passing many (20-50) variables into the function, rather than just 3 above, it seems very inefficient to pass that many arguments, and perhaps there might even be a memory issue if I pass too many of them.

So my actual question is what is the best method to efficiently pass a ridiculous amount of arguments into a PHP function.

I don't want to ask you guys to post finished code, although I welcome any snippets you could provide, but really I just want to know what you guys think the best practice would be to pass an extreme number of arguments into a function.

Update/Clarification: To be more specific, the purpose for needing so many arguments, is because the values are required for an API request. Rather than writing the same code over and over, I am writing a function that I can call to make the API request as needed, but I need to pass the arguments/variables into the function, and then place them into the array, which is then sent with the API request. All of the values are very small strings, or numerical values.

3
  • 9
    Passing 20 arguments will surely not "cause a memory issue". It's very bad style, though. Consider passing an (associative) array instead. Jul 8, 2014 at 18:09
  • 3
    Hard to say for you specific case, but in general, it's a sign of bad design if you need so many parameters. Maybe there's a different structure. Often, this problem can be solved by using a pattern like the builder pattern, but there is not enough information to tell whether that's the case here.
    – GolezTrol
    Jul 8, 2014 at 18:10
  • @Mastrianni See Variadic Functions in my answer below
    – gpupo
    Jul 9, 2014 at 12:54

3 Answers 3

4

I believe the best way to get complex values ​​either through dependency injection, using a more advanced programming model, especially object-oriented

function shortexample(\stdClass $args) {
    $body = array(
        'username' => $args->username,
        'password' => $args->pass,
        'campaign_id' => $args->id,
    );
}

You should improve the definition of objects that will be received, using interfaces

//....
interface shortExampleInterface {
    public function getUsername();
    public function getPass();
    public function getId();
}



function shortexample(\shortExampleInterface $args) {
    $body = array(
        'username' => $args->getUsername(),
        'password' => $args->getPass(),
        'campaign_id' => $args->getId(),
    );
}

But I see that as the context may actually require very parameters, it is worth trying the new feature of PHP 5.6:

Variadic Functions

This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:

function concatenate($transform, ...$strings) {
    $string = '';
    foreach($strings as $piece) {
        $string .= $piece;
    }
    return($transform($string));
}

echo concatenate("strtoupper", "I'd ", "like ",
    4 + 2, " apples");

The parameters list in the function declaration has the ... operator in it, and it basically means " ... and everything else should go into $strings". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array, ready to be used.

Example 2:

function f($req, $opt = null, ...$params) {
    // $params is an array containing the remaining arguments.
    printf('$req: %d; $opt: %d; number of params: %d'."\n",
           $req, $opt, count($params));
}

f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
f(1, 2, 3, 4, 5);
4
  • I think this answer is a too generic answer for a too generic question. Not my downvote, though. I do agree with your line of thinking. -edit- I see the code addition now, and I don't really agree with that. stdClass doesn't allow for type checking. I would rather use a specific class or interface.
    – GolezTrol
    Jul 8, 2014 at 18:13
  • I was still writing :D
    – gpupo
    Jul 8, 2014 at 18:19
  • Cheerio! That's better. ;)
    – GolezTrol
    Jul 8, 2014 at 19:17
  • 1
    Check Variadic Functions on PHP 5.6 New features php.net/manual/en/migration56.new-features.php
    – gpupo
    Jul 9, 2014 at 12:55
1

Pre PHP 5.6 you can use func_get_args() to get an array of all arguments passed. You can also use call_user_func_array() to pass unpack an array of arguments to be passed to a function.

0

Pass an Associative array that will be the function list. Each key will be the name of the argument ($id, $name $etc) and the value will be the value you'd like passed for that argument.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.