Re: [PHP-DEV] Named parameters

2010-10-18 Thread mathieu.suen

On 10/15/2010 07:26 PM, G M wrote:

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());
}
   


Why not change the parser to accept also this:

function stuff($a, $b, optional($separator = 1, $clean = false))
{

}


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
   



-- Mathieu Suen





Re: [PHP-DEV] Named parameters

2010-10-18 Thread Brian Moon
This is just not happening people. Please re-read all the past 
conversations about it. If you come to some sort of epiphany that solves 
all of the issues brought up in the past, please submit a patch. 
Otherwise, a never ending thread about this *again* is pointless.


Thanks,

--

Brian.

http://brian.moonspot.net/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Named parameters

2010-10-18 Thread Richard Lynch
On Mon, October 18, 2010 2:45 am, mathieu.suen wrote:
 On 10/15/2010 07:26 PM, G M wrote:
 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);

When I suggested this to Ze'ev in PHP 3.x - 4.0 days, I believe his
response ran along the lines of running away screaming into the
night...

After a decade of reflection, I think it over-complicates PHP, which
we don't really need.

Just pass in an array with whatever parameters you want, and tear it
apart in the function.

You can @document that array in any way you choose, naming all the
valid keys, and what they ought to be.

-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=FS9NLTNEEKWBE



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php