Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60218 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25728 invoked from network); 19 Apr 2012 13:33:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2012 13:33:15 -0000 X-Host-Fingerprint: 208.107.13.98 host-98-13-107-208.midco.net Date: Thu, 19 Apr 2012 09:33:15 -0400 Received: from [208.107.13.98] ([208.107.13.98:20344] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/21-18164-B14109F4 for ; Thu, 19 Apr 2012 09:33:15 -0400 Message-ID: <55.21.18164.B14109F4@pb1.pair.com> To: internals@lists.php.net References: <4F8DF4B1.2040307@sugarcrm.com> User-Agent: slrn/pre1.0.0-18 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit X-Posted-By: 208.107.13.98 Subject: Re: [PHP-DEV] [RFC] skipping optional parameters From: weierophinney@php.net (Matthew Weier O'Phinney) On 2012-04-19, Yasuo Ohgaki wrote: > Just a though for named parameter, since it seems > its becoming a relevant topic now. > > 2012/4/18 Daniel Macedo : > > 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 | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc