

I want to put named arguments into PHP. Thoughts? - EGreg

Okay so I am thinking about submitting a patch to PHP that would enable you to call functions like this:<p>stuff(1, 2, 'separator' =&#62; '&#60;br&#62;', 'clean' =&#62; true);<p>and define them like this:<p><pre><code>  /**
  * function to do stuff
  * @param integer $a
  * @param integer $b
  * @param string $separator
  *   Optional, you can override the separator
  * @param boolean $clean
  *   Optional, if true then things are done more cleanly
  * @param integer $cool
  *   Optional. Overrides the default coolness level of 7.
  */
  function stuff($a, $b)
  {
     var_export(func_get_args());
  }
</code></pre>
so that the call<p><pre><code>  stuff(1, 2, 'separator' =&#62; '&#60;br&#62;', 'clean' =&#62; true)
</code></pre>
will output<p><pre><code>  array(1, 2, 'separator' =&#62; '&#60;br&#62;', 'clean' =&#62; true)
</code></pre>
see the consistency? :)<p>The way I plan to do this is by changing the parser to notice arguments passed like 'a' =&#62; 'b' at the end of the argument list, and pass a hidden array variable on the stack, so when func_get_args() is called, it will merge this array on top of what func_get_args would return. I also want to make another function, func_extract_args which will basically extract the optional arguments, so I can more easily refer to them as $separator, $clean, etc.<p>What do you guys think of this? Would it be useful to you? I really hope for this feature to make it into PHP 6, so I might sit down and code it myself, to show it can be done.<p>The reason I really want this to make it into PHP 6 is because I noticed that function parameter lists just grow with time, and they provide a poor mechanism for designing nice interfaces. For example I'd hate to write this:<p>stuff(1, 2, null, '&#60;br&#62;', true, null)<p>as it provides no clue as to what the values are for, and also there are lots of extra "null" values I had to pass just so I can specify some optional values. And if the function had defaults like this:<p>stuff($a, $b, $c = null, $separator = "\n", $clean = false, $cool = 7, $more = array())<p>then that would be even worse, as I'd have to re-type the non-null, and if they ever changed, I'd be out of luck.<p>All this is solved in a way that extends the array( ) syntax to function calling (well, almost ... I think it would be cleaner to put all optional arguments at the end of the argument list, as opposed to interspersing them).<p>What do you guys think? Would this be useful to you? I personally would use it all the time and wonder how PHP was without it, but perhaps other people couldn't care less? Maybe it has some negative side effects that I haven't thought ot?<p>Greg
======
AlexC04
can you not already pass in a named array ?

function stuff($a, $b, $sep = null, $clean = null) {

    
    
      // wooop
    
    

}

$params = array('a' => 1, 'b' => 2, 'clean' => true);

stuff($params);

I've never actually played with that directly myself, but I think I've seen it
"out of the corner of my eye" when looking at code written by others.

Maybe I imagined it.

~~~
EGreg
No, you can't. You may be thinking of call_user_func_array, but once again you
can indexed array.

The most you can do -- which is what I am doing now -- is use the design
pattern:

function stuff($required1, $required2, $options = array()) {

}

This pattern works really well, and makes the function interface endlessly
extensible. If we implemented named parameters in PHP, it would turn every
EXISTING function into such a pattern, which would make functions evolve
nicely instead of becoming monstrosities with default parameters in an
arbitrary order:

stuff(1, 2, null, true, null, 0, array(), '<br>')

------
EGreg
I'd be happy if someone else was also interested in this feature and we could
do this together. I'm new at hacking PHP internals.

