3 ms·
I want to put named arguments into PHP. Thoughts?
Okay so I am thinking about submitting a patch to PHP that would enable you to call functions like this:
stuff(1, 2, 'separator' => '<br>', 'clean' => true);
and define them like this:
/**
* 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());
}
so that the call
stuff(1, 2, 'separator' => '<br>', 'clean' => true)
will output
array(1, 2, 'separator' => '<br>', 'clean' => true)
see the consistency? :)
The way I plan to do this is by changing the parser to notice arguments passed like 'a' => '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.
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.
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:
stuff(1, 2, null, '<br>', true, null)
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:
stuff($a, $b, $c = null, $separator = "\n", $clean = false, $cool = 7, $more = array())
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.
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).
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?
Greg
- EGreg 16y agoI'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.
- AlexC04 16y agocan 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 16y agoNo, 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>')