Hi,

Just a though for named parameter, since it seems
its becoming a relevant topic now.

2012/4/18 Daniel Macedo <admac...@gmail.com>:
> I'll keep going offtopic a bit more.
> I would rather see named parameters implemented *prior* to this.
> Although maybe not instead, I think both might have their place.
>
> On Wed, Apr 18, 2012 at 08:43, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:
>>
>> Something like
>>
>> setcookie( name:'mycookie', value:'abc', secure:true, httponly:true );
>>
>> is really nice to have. Source code will be much more readable.
>>
>
> I agree with this! But for short array syntax we kept the => as in
> $array = ["foo" => "bar"];
> Not sure if this was a limitation, lack of that suggestion or a
> decision; but the shortest syntax it's still not... (as Yoda would
> say!)
>
> $array = ["foo": "bar"]; doesn't look weird to me, plenty readable,
> and that's the shortest!

Object can be used as named parameter in JavaScript.

// Define function to take one "argument", which is in fact an object:
function fnParseInt( oArg ){
   return parseInt( oArg.number, oArg.radix );
}
// Which you then call like this (pass in an object literal):
fnParseInt( { number : 'afy', radix : 36 } );

If there is a JSON like syntax for PHP objects, named parameter
can be implemented just like this. It would not work for function
that accepts object as first parameter, but who cares?

Pros.
  - simple
  - everyone are used to JSON and JavaScript now a days
  - supporting JSON syntax is convenient for other purpose
  - internal functions may support this.
     (Single object parameter for this)
  - coexists func($a,,,,$b)

Cons
  - loose type hints
  - loose default values for params
  - yet another script injection risk if we do "eval($json_data)"
    (json_decode() should be used, anyway.)

We may use array to emulate JavaScript, though.
fnParseInt(['number'=>'afy', 'radix'=>36]);

It's many typing compare to other languages, but it works.
If we are going to have array like syntax, we may  just
use array.

Anyway, I'm not sure what happened for annotation.
Annotation maybe used as type hints, etc.

Just an idea that I think of now.
There might be many better ideas for named parameter.

Regards,

--
Yasuo Ohgaki
yohg...@ohgaki.net

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

Reply via email to