0
function fu($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null){
   echo 'helo world';
}
//can be other defaults instead null;

fu('arg1');
fu(,,'arg3');
fu(,'arg2',,'arg4');

how can I call fu if any arguments possibly can be missed? I can't use fu(null,'arg2',null,'arg4'); because default for arg1, arg3 can contein inf

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
scrache
  • 113
  • 1
  • 1
  • 8
  • The answers are here, but I think you need to be more specific about your question because we're at a loss to what you really want... – Raphioly-San Jan 12 '16 at 09:21

6 Answers6

2

You have a few options to handle this case. Here the 2 I can think about.

Using an array

With an array you don't bother with how many arguments your function receives, and each argument is named (with the key in the array)

function fu($param = array()) {
   // you can test here is $param['arg1'] exists, $param['arg2'] and so on
   echo 'helo world';
}

Using func_get_args

This way you can how many arguments you like to the function, but they aren't named

function fu()
{
    foreach (func_get_args() as $index => $value) {
        echo "Argument {$index} is: \n", var_export($value);
    }
}
cEz
  • 4,932
  • 1
  • 25
  • 38
ôkio
  • 1,772
  • 1
  • 15
  • 16
  • I believe `func_get_args` is not a good solution if the function is called more than 2 times with the same _default_ arguments. – Denis Alexandrov Jan 12 '16 at 09:19
  • 2
    The one asking the question also isn't clear about what his intentions are. This answer is fine if you are looking for a function that can accept multiple arguments/parameters – Raphioly-San Jan 12 '16 at 09:44
0

Just put '' in place of missed arguments for function call.

fu('','','arg3','arg4');

0

You probably coming from another programming language like Python, but in PHP you cannot skip an intermediate argument like this. The optional parameters should be last. However you can pass a named array to achieve what you want. Something like this:

function fu ($arr =[]) {
    foreach ( [1, 2, 3, 4] as $key )
        $arr ["arg$key"] =isset($arr["arg$key"]) ? $arr["arg$key"] : null;

    print_r ($arr) ;
    echo "<br />";
}

fu (['arg1' => 'arg1']) ;
fu (['arg3' => 'arg3']) ;
fu (['arg2' => 'arg2', 'arg4' => 'arg4']) ;
cyrille
  • 2,616
  • 1
  • 10
  • 18
0

another possible solution, expanding the array approach:

function fu($inputArgs = array()) {
   $defaultArgs = array('arg1' => 'x', 'arg2' => 'y');
   $args = array_merge ($defaultArgs, $inputArgs);

   //either use $args['arg1'] or extract the values:
   extract($args);
   // now you can use $arg1, $arg2... directly
   ...
}

you can enter a non-default value by using the same key in the $inputArgs array, for example :

fu(array('arg2' => 'z'));
cypherabe
  • 2,562
  • 1
  • 20
  • 35
0

You can use func_get_arg() if your arguments are not fixed.

function fu(){
  $args = func_get_args();
  print_r($args);
}

Now you can pass anything like fu() or fu('1','null',null) or fu(null,'arg2',null,'arg4'); etc.

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

Languages such as Java offer multiple variations of the same method to overcome this but in PHP only one variation is possible. For arguments that will not be used you will have to use the default argument values as arguments.

fu('arg1');
fu(null, null, 'arg3');
fu(null, 'arg2', null, 'arg4');

My advice on methods with too many optional arguments is either your code is too complicated and may profit from a refactoring such as creating a class and passing the arguments to setters or if you want to implement a function with no preset amount of arguments, it would probably make sense to use func_get_args();