On 2012-04-19, Yasuo Ohgaki <yohg...@ohgaki.net> wrote: > Just a though for named parameter, since it seems > its becoming a relevant topic now. > > 2012/4/18 Daniel Macedo <admac...@gmail.com> : <snip> > > 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?
This is the workaround many of us are already using, though typically passing an array, not an object. With 5.4, the notation is almost usable, as you can simply do the square brackets: fnParseInt([ "number" => "afy", "radix" => 36 ]); > 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 While the solution is indeed simple, the cons loom very large -- you end up needing to perform a lot of logic internally in the function in order to support default values as well as enforce types. That kind of boilerplate gets very old after a while. Named parameters would be ideal, but being able to skip parameters will definitely work in the interim. -- Matthew Weier O'Phinney Project Lead | matt...@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php