0

I'd like to just modify the second default param in a function but I don't know how to

function test($a, $b = "b", $c = "c"){
    echo $a." ".$b." ".$c;
}

test("a");
test("a","z","e");
test("a","z");

I'd like for exemple to use this function keeping $b as default and changing $c. I try

test("a",,"f");

But it doesn't work.

Charles
  • 50,943
  • 13
  • 104
  • 142
Ajouve
  • 9,735
  • 26
  • 90
  • 137
  • You just can't do that... you have to pass again the $b default value. The common use of default parameter is that the less set variable is set to the far right, for your example that mean $a will always be set, $b will be set sometimes and $c will be set rarely and so on – Jaay Jun 11 '13 at 12:18

2 Answers2

0

You can try ,

function test($a, $b = "b", $c = "c"){
    echo $a." ".$b." ".$c;
}

 function get_default_param($fn)
 {
 $function = new ReflectionFunction($fn);
 $default=array();

  foreach ($function->getParameters() as $param) {
  if ($param->isOptional()) {
    $default[]=$param->getDefaultValue() ;
}else{
 $default[]='';
}

}
return $default;
}


$default=get_default_param('test');//for getting all default parameters of 'test' as an array
test("a",$default[1],"f");

Output

 a b f
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • It is correct, yes, but doing it this way is too complicated to use it as an easy workaround I feel. – bwoebi Jun 11 '13 at 12:40
  • @bwoebi,Once we created get_default_param(),Then we can use easly.I dont know it is posible to do with an easy way – Shijin TR Jun 11 '13 at 12:42
  • And that's also my opinion: there's no easy way. And this way costs a lot performance (though I cannot think of a better way) – bwoebi Jun 11 '13 at 12:45
0

This is not possible in PHP nowadays. You have to pass the default value manually like:

test("a", "b", "f"):

There exists a RFC about this: https://wiki.php.net/rfc/skipparams But it hasn't had any success yet.

bwoebi
  • 23,637
  • 5
  • 58
  • 79