Re: Named arguments revisited

From: Date: Thu, 01 Dec 2005 18:45:56 +0000
Subject: Re: Named arguments revisited
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-20972@lists.php.net to get a copy of this message
On Dec 1, 2005, at 6:59 AM, Christian Schneider wrote:
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.
Well, I wouldn't say that. The problem isn't that positional parameters don't work fine -- they do -- but that upon code inspection you don't know what those parameters are for unless you know the function/method API ahead of time. Sure, as you pointed out there are some juicy features you get with named parameters if you implement them throughly, but I still say its main selling point is that the calling code is its own API documentation. Regards, Jared

« previous php.internals (#20972) next »