The optional argument in php must be followed by required arguments?

126 4
public function addHost($groupId, $nodeId ="node_0", $role ="master", $hostConfig)
{
//.....
}

In my image is always a required parameter definition before the optional argument, the code above is the source code seen in lotusphp, so how to define how to use it.

3 Answers

127 1
function test($a, $b = 3, $c = 4, $d) {
 echo $a.$b.$c.$d;
}
echo test(4,"","", 9);
//打印结果
//49
echo test(4, null, null, 9);
//打印结果
//49

In fact, $ b = 3, $ c = 4 defaults to never set success.
If the fourth parameter is a required parameter, you must have four more than four arguments when using the function, the default parameter isn't meaningful.

...