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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to