Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:20935 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77904 invoked by uid 1010); 1 Dec 2005 08:20:12 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 77888 invoked from network); 1 Dec 2005 08:20:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Dec 2005 08:20:11 -0000 X-Host-Fingerprint: 194.109.253.196 mediawave.xs4all.nl Received: from ([194.109.253.196:7786] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id 2B/8A-14828-B32BE834 for ; Thu, 01 Dec 2005 03:20:11 -0500 Message-ID: <2B.8A.14828.B32BE834@pb1.pair.com> To: internals@lists.php.net Date: Thu, 01 Dec 2005 09:20:55 +0100 User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 References: <374c2968dd7f3d13c696037ef850256f@gravitonic.com> <2D.57.14828.3453E834@pb1.pair.com> <23.98.14828.81AAE834@pb1.pair.com> In-Reply-To: <23.98.14828.81AAE834@pb1.pair.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 194.109.253.196 Subject: Re: [PHP-DEV] Named arguments revisited From: bart@mediawave.nl (Bart de Boer) I have to admit, although you could do it other ways, it wouldn't be as clean as what you're proposing. Especially handy for when you just want to pass on that last parameter without having to pass on all the forgoing parameters. I'd consider it a nice-to-have. As for the syntax. I'd like to propose it as follows (syntax-wise): somefunction($var, $var2, $var3, $var4 = 'val4', $var5) {} Syntax for calling it with named arguments: somefunction($var3 = 'val3', $var5 = $othervar = 'val5'); Or it may be confusing to use '$' in front of the names. (I was looking for consistency with the way you define a function.) So perhaps like this: somefunction(var3 = 'val3', var5 = $othervar = 'val5'); Only now I'm wondering. Since the PHP engine would have to check for named arguments for each function. Wouldn't this performancewise cause too much overhead on function calls? Ron Korving wrote: > You could use an associative array, but then you have a not-so-clean syntax > and you have to handle default values for missing parameters yourself. > > Named parameter example: > > function adduser(username: $username, password: $password, superuser: > $superuser=false) > { > // now do some stuff with $username, $password and $superuser > } > > adduser('root', 'abcdefg', true); > // or: > adduser(username: 'root', password: 'abcdefg', superuser: true); > ?> > > Traditional named example: > > function adduser($params) > { > if (!is_array($params)) throw new Exception('No named parameters'); > if (!isset($params['username'])) throw new Exception('Missing parameter: > username'); > if (!isset($params['password'])) throw new Exception('Missing parameter: > username'); > if (!isset($params['superuser'])) $params['superuser'] = false; > > // now do some stuff with $params > } > > adduser(array('username' => 'root', 'password' => 'abcdefg', 'superuser' > => true)); > ?> > > You see the big advantages of named parameters? > - clean syntax > - no array handling inside the function or method > - no checking on the existance or non-existance of parameters > - no forcing default values for missing parameters > - when you need to skip a parameter, you no longer have to give it's default > value when calling the function, you can simply skip the whole parameter: > > function foo(bar: $bar=0, bla: $bla='test', cow: $moo='moooo'); > > call: > foo(cow: 'test'); > foo(0, 'test', 'test'); > > > Named parameters would kick serious butt :) > > - Ron > > > > "Bart de Boer" schreef in bericht > news:2D.57.14828.3453E834@pb1.pair.com... > >>Hi Jared, >> >>If probably don't understand named arguments correclty but couldn't you do >>something like: >> >>function(array('name1' => 'val1', 'name2' => $var)); >> >>In the function you could then check which keys (names) have values, >>thereby simulating a form of named agruments? >> >> >> >> >>>On Nov 29, 2005, at 11:17 PM, Jared White wrote: >>> >>> >>>>Named arguments are absolutely essential for using PHP as a solid >>>>templating language, and, in fact, they also greatly enhance code >>>>readability for complex method calls of in-depth APIs. My experience >>>>with both Objective-C and Python has showed me the wonders and joys of >>>>named arguments, and it is something I've desperately wanted in PHP for >>>>ages. I'm sure I'm not alone in this. I've tried array constructs, >>>>multiple arguments with string-based names and fancy parsing using >>>>func_get_args(), and various combinations thereof, and nothing is a good >>>>substitute for the real deal.