Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:20951 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55993 invoked by uid 1010); 1 Dec 2005 14:59:20 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 55978 invoked from network); 1 Dec 2005 14:59:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Dec 2005 14:59:20 -0000 X-Host-Fingerprint: 195.141.85.117 uf1.search.ch Linux 2.4/2.6 Received: from ([195.141.85.117:53402] helo=verksam.search.ch) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id C1/7F-14828-7CF0F834 for ; Thu, 01 Dec 2005 09:59:20 -0500 Received: from localhost (localhost [127.0.0.1]) by verksam.search.ch (Postfix) with ESMTP id B111A3A01C7; Thu, 1 Dec 2005 15:59:16 +0100 (CET) Received: from unknown by localhost (amavisd-new, unix socket) id client-XXufOe95; Thu, 1 Dec 2005 15:59:16 +0100 (CET) Received: by verksam.search.ch (Postfix, from userid 65534) id E05263A049D; Thu, 1 Dec 2005 15:59:15 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.1.0 (2005-09-13) on verksam.search.ch X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.1.0 Received: from [192.168.1.72] (ultrafilter-i [192.168.85.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by verksam.search.ch (Postfix) with ESMTP id 12C963A01C7; Thu, 1 Dec 2005 15:59:15 +0100 (CET) Message-ID: <438F0FC0.5070407@cschneid.com> Date: Thu, 01 Dec 2005 15:59:12 +0100 User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050715) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Ron Korving Cc: internals@lists.php.net 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-Virus-Scanned: by amavisd-new at search.ch Subject: Re: [PHP-DEV] Named arguments revisited From: cschneid@cschneid.com (Christian Schneider) Ron Korving wrote: > Named parameter example: Your example misses the main advantage of named parameters IMHO: Sets of parameters you don't want to or can't explicitely list because they are not know yet. > 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)); This is not how we use named parameters at all. If you have a finite set of fixed parameters then positional parameters are working just fine. One of the main advantages lies in a catch-all parameter which gets all unassignable parameters. While this opens the door for typos even a whips'n'chains language like Python added this feature because it is just too useful to omit if you have named parameters :-) Our use case is something along the lines of (not the actual code, so don't comment on anything specific here ;-)): function tag($name, $p = array()) { foreach ($p as $key => $value) { if (is_int($key)) $content .= $value; else $attributes .= " $key='$value'"; } return "<$name$attributes>$content"; } function a($p = array()) { return tag("a", $p); } ... echo a(array('href' => $url, 'title' => $title)); or with our patch to make array() obsolete echo a('href' => $url, 'title' => $title); Now something like echo a(href: $url, title: $title, 'non-identifier-chars': 42); would be even neater, agreed ;-) - Chris