简体   繁体   中英

How to pass arguments to a function without repeating them so many times?

I'm trying to avoid this:

function do_something($arg1, $arg2, $arg3, $arg4, $arg5) {

   function do_something_else($arg1, $arg2, $arg3, $arg4, $arg5) {
      // code    
   }

   do_something_else($arg1, $arg2, $arg3, $arg4, $arg5);

   // do other things with the args

}

That is, repeating the args over and over especially in situations when there are a lot of args. In this example I need to have a nested function called do_something_else inside the main do_something function. The nested function also makes use of all the same args passed into it's parent. So I must pass those into it and once more repeat all those args when actually executing the do_something function.

Is there a cleaner way to handle this? I know the built in php function func_get_args() returns an array of all the arguments inside a function but how to pass these into a child function so that they are available there as well, basically looking for a way to do this without repeating the args so many times?

将您的参数放在一个对象中。

You already said it. You can use func_get_args() for simplicity. Combine it with call_user_func_array() and you're done:

call_user_func_array("do_something_else", func_get_args());

Actually there is a small caveat. This will only work in PHP 5.3 and onwards. Before that you need a temporary variable:

$args = func_get_args();
call_user_func_array("do_something_else", $args);

So, it's not much more concise really. But it already shows the alternative: just refine your function signature to use arguments from an array instead.

function do_something($arg1, $arg2, $arg3, $arg4, $arg5) {

  function do_something_else($arg1, $arg2, $arg3, $arg4, $arg5) {
    echo $arg5;
  }

  call_user_func_array('do_something_else', func_get_args());
}

This would only save you one repetation though, namely on calling the do_something_else function.

You can use data tranfer object ( DTO ). DTO is an data structure(struct or class ) that uses to transfer data between layers or functions.

For a nested function, you can also let the closure use variables from the surrounding context, using the "use" keyword. I do this quite frequently:

$function = function($var) use ($context1, $context2) {
    // code
}
$function($input1);
$function($input2);

It does require PHP 5.3 or later.

You could always just pass an associated array. Allows for flexibility.

function sample($data = array()) {
   print_r($data);
}

sample(array('id'=>123.'category'=>'tv'));

You basically give the answer by mentioning func_get_args() .

function do_something($arg1, $arg2, $arg3, $arg4, $arg5) {

   function do_something_else($args) {
      // code    
   }

   do_something_else(func_get_args());

   // do other things with the args

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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