Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60217 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25649 invoked from network); 19 Apr 2012 13:33:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2012 13:33:13 -0000 X-Host-Fingerprint: 208.107.13.98 host-98-13-107-208.midco.net Date: Thu, 19 Apr 2012 09:33:12 -0400 Received: from [208.107.13.98] ([208.107.13.98:12082] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 45/21-18164-714109F4 for ; Thu, 19 Apr 2012 09:33:12 -0400 Message-ID: <45.21.18164.714109F4@pb1.pair.com> To: internals@lists.php.net References: <4F8DF4B1.2040307@sugarcrm.com> User-Agent: slrn/pre1.0.0-18 (Linux) X-Posted-By: 208.107.13.98 Subject: Re: [PHP-DEV] Re: [RFC] skipping optional parameters From: weierophinney@php.net (Matthew Weier O'Phinney) On 2012-04-19, Patrick ALLAERT wrote: > 2012/4/18 Matthew Weier O'Phinney : > > My one comment, which others have raised, is readability of multiple > > commas -- TBH, at first glance it has the appearance of a mistake. I > > think those suggesting a keyword such as "default" make a good point in > > this regard -- it makes it 100% clear that you want the default value > > for the argument in that position. This also presents an improvement > > over current usage, as you're not hard-coding values in your function > > calls themselves -- particularly as the defaults could change in future > > refactors. > > I think we should only support optional parameters using the "default" > keyword as it would probably make things more consistent and less > error prone: > > function foo( $bar = "bar", $foo = "foo", $baz = "baz" ) { ... } > > foo(,,, "myBaz"); // Thinking changing the value of $baz, but there's > one too much "," and PHP won't complain because of too many arguments > used! > > Additionally, we might also want to think about > call_user_func()/call_user_func_array(). > > If for the former it could work with: > > call_user_func( "foo", , , "myBaz" ); > > What about the latter? > > call_user_func_array( "foo", [2 => "myBaz"] ); // ? Evaluating the > element at index 0 would cause a notice, but would result in a NULL, > so I would say that NULL is to be used as first parameter. I actually would argue you shouldn't skip parameters when using call_user_func(); even with call_user_func_array(), I'd argue that named parameters is the only way I'd want to allow skipping parameters. The reason I argue this is because when dynamically calling a function, you typically don't actually know exactly what the function/method is -- and thus making any assumptions about the signature other than required parameters is simply begging for problems. > Last but not least: if skipping optional parameters is implemented, > should we consider the support of default values on the leftmost side > of functions? > > function foo( $bar = "bar", $baz ) { ... } > > It could make sense that $bar followed by $baz is semantically better > than the opposite for technical reason, and this could be called > using: > > foo( default, "baz") > or: > foo(, "baz") I'd argue no -- as required arguments should always have precedence over optional ones in the signature when it comes to position. -- 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