0

This is more curiosity than pragmatic.

That said, the below function accepts 3 arguments the second and third of which have default values and are optional.

   function doStuff( $param1, $param2='default', $param3='value' ){
             // do stuff ....
    }

Is there a way to call doStuff and provide values for $param1 and $param3 but have $param2 retain its default value

Im thinking something like doStuff('new',$placeholder,'value')

Where inside the function the values would now be 'new','default','value'

Is this possible, or must you explicitly pass a value for 2 to get to 3?

Like I say, I'm more curious than actually needing this.

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • 2
    No, it's not possible *easily*. You could employ reflection to construct argument default values automatically, but that's a lot of overhead... – deceze Apr 12 '16 at 14:57
  • @deceze yeah I was skeptical, since I have your ear, would passing in an associative array and merging it with an array of defaults be practical, like you might do in a jQuery plugin? – Wesley Smith Apr 12 '16 at 15:01
  • 1
    It's certainly *a* solution, but that fundamentally changes the function's signature, and now you're relying on informal and probably badly documented conventions about stuff that may or may not be in an array. Don't do this as a general solution, only as appropriate. – deceze Apr 12 '16 at 15:03

0 Answers0