Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10737 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99834 invoked by uid 1010); 23 Jun 2004 21:58:41 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 99774 invoked from network); 23 Jun 2004 21:58:41 -0000 Received: from unknown (HELO amsfep12-int.chello.nl) (213.46.243.17) by pb1.pair.com with SMTP; 23 Jun 2004 21:58:41 -0000 Received: from [192.168.0.2] (really [213.93.244.198]) by amsfep12-int.chello.nl (InterMail vM.6.00.05.02 201-2115-109-103-20031105) with ESMTP id <20040623215840.LBDM20358.amsfep12-int.chello.nl@[192.168.0.2]>; Wed, 23 Jun 2004 23:58:40 +0200 Message-ID: <40D9FD0F.2060204@chello.nl> Date: Wed, 23 Jun 2004 23:58:39 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616 X-Accept-Language: en-gb, en-us, en, nl MIME-Version: 1.0 To: Daniel Crookston CC: Bert Slagter , internals@lists.php.net References: <20040623150429.E62456@hyperion-data.net> <20040623163305.73484.qmail@pb1.pair.com> <20040623194751.N22728@hyperion-data.net> In-Reply-To: <20040623194751.N22728@hyperion-data.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: keyword arguments? From: jdmaas@chello.nl (Jochem Maas) Daniel Crookston wrote: > The major benefit of keyword arguments doesn't occur when you're > writing functions, it occurs when you're re-writing them. I can't > count the number of times where I've thought "My xyz function already > does something almost exactly like what I need... if I just passed it > an extra parameter, I could rewrite xyz to do what I need and save a > lot of time." So I add another parameter, making it optional. > > This is fine, all the calls to xyz that are lacking the final > parameter still work (since it's optional.) But do this once or > twice, and pretty soon you have function calls that look like this: > > xyz('a', 2, $foo, '', '', '', '', '', '', $bar); > > That's ugly and unnecessary, and leaves lots of room for bugs when > you're writing functions that use the nth optional parameter in your > function (since you have to count the number of blank spaces you need > to leave.) > > Additionally, what happens when all of your original, three-argument > (a, 2, $foo) calls to xyz need to start passing an additional argument > because of a change you couldn't predict when you first wrote your > function? all that can be offset by write a complementary set of wrapper functions like: function xyzSuperSpecialFooBar($a, $b, $foo, $bar) { /* using the 7th optional arg - first 3 args required (or whatever) */ return xyz($a, $b, $foo, '', '', '', '', '', '', $bar); } which also gives you some nice sugar to help you remember exactly what that variation of params is supposed to do. > > Keyword arguments solve these problems. They're always optional - not > just optional in that you can stick a '' or NULL in the spot where you > would put a value in the function call. They're optional in that you > can leave them out entirely. Because they're named, they can also be > passed in any order. And, you function is still going to have to do some checking for all these optional args is it not? whats the great benefit of the optional named args in any order vs. passing an array ala: $arr = array( 'arg1' => 1, 'arg2' => 2, 'arg3' => 4, ); $rtnV = mySwissArmyKnife($arr); ---- where : function mySwissArmyKnife($arr = array()) { extract($arr); ... do the checking... ... calc something ... return $x; } > finally, you can add new keyword arguments at any time, and none of > your current function calls will break. > > If there's a way to do this in PHP (short of having the last argument > be a hash) that I've missed, please let me know about it. how about look at the problem in a different way - is the function xyz() (I assume that you have some realworld situation where this kind of thing would handy right now) possibly a candidate for spliting up into a class? with regard to the 'swiss army knife' function.. have you ever tried to do any serious construction work with a swiss army knife ;-) seriously tho, from what I gather there is a icecubes chance is hell that this will be implemented. > > Daniel Cr > > On Wed, 23 Jun 2004, Bert Slagter wrote: > >> Even after reading your message twice, I can't think of an example >> where this would be useful. Obviously, I don't understand your >> intention. >> >> Could you give an example of a construction that is made possible by >> this feature (and thus would otherwise be impossible)? >> >> Bert > >