Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] Named arguments revisited [RSS Feed]

#1 Nov. 30, 2005 07:17:42

Jared W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


Hi folks,I just got on the list -- I've been a big fan of PHP for severalyears and am throughly enjoying PHP 5. Good work folks, and once Ican get 5.1 set up on my OS X box I'm sure it's be even better.I've been very interested in hearing about PHP 6 feature discussions,and the meeting notes that Derrick posted looked really good. Butthere was one item that stood out for me as being extremelydisappointing, and that was the curt dismissal of named arguments (orparameters I guess you are calling them).Named arguments are absolutely essential for using PHP as a solidtemplating language, and, in fact, they also greatly enhance codereadability for complex method calls of in-depth APIs. My experiencewith both Objective-C and Python has showed me the wonders and joysof named arguments, and it is something I've desperately wanted inPHP for ages. I'm sure I'm not alone in this. I've tried arrayconstructs, multiple arguments with string-based names and fancyparsing using func_get_args(), and various combinations thereof, andnothing is a good substitute for the real deal.If I had the technical knowhow to jump in and help implement this, Iwould, but I'm a lousy C programmer. :( But I definitely want to lendmy support to anyone interested in working on this. Please, pleasedon't give up on this feature!Thanks so much,

Jared

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Nov. 30, 2005 21:52:33

Andrei Z.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


Can you explain your reasoning behind "essential for using PHP as asolid templating language" and "nothing is a good substitute for thereal deal"?- Andrei

On Nov 29, 2005, at 11:17 PM, Jared White wrote:Named arguments are absolutely essential for using PHP as a solidtemplating language, and, in fact, they also greatly enhance codereadability for complex method calls of in-depth APIs. My experiencewith both Objective-C and Python has showed me the wonders and joys ofnamed arguments, and it is something I've desperately wanted in PHPfor ages. I'm sure I'm not alone in this. I've tried array constructs,multiple arguments with string-based names and fancy parsing usingfunc_get_args(), and various combinations thereof, and nothing is agood substitute for the real deal.--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#3 Nov. 30, 2005 23:17:32

Jared W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


On Nov 30, 2005, at 1:50 PM, Andrei Zmievski wrote:Can you explain your reasoning behind "essential for using PHP as asolid templating language" and "nothing is a good substitute forthe real deal"?- AndreiOK, to take an example from Smarty, you could do a value cycle (formulti-row-color tables, etc.) with some extra parameters like so:{cycle name="myCycle" values="#eeeeee;#d0d0d0" print=falsereset=true delimiter=";"}(I'm not a Smarty expert, so I apologize if I didn't get that quiteright.)Now, if I wanted to express that with a PHP function currently, itmight look like this:cycle("myCycle", "#eeeeee;#d0d0d0", false, true, ";");My question is, what if you have no idea how the cycle functionworks, or you haven't used it in a while and temporarily forgot? Yourtwo options are look at the source code (if possible) or look at thedocumentation (if it's any good). Whereas with named arguments, it'sself-explanatory.cycle(name: "myCycle", values: "#eeeeee;#d0d0d0", print: false,reset: true, delimiter: ";");(FYI: I just picked a colon for the heck of it...whatever operator isused isn't important to me.)Much, much clearer and obvious. Perhaps not such a big deal in"regular" PHP code blocks, but in an HTML/PHP mixed template type ofscenario, the named arguments are so much nicer. Plus, if they'reimplemented in an order-agnostic fashion, you could reorder thosearguments any way you like -- but I don't necessarily think that'sits biggest selling point.Sure, you could use an array for this, like so:cycle(array("name" => "myCycle", "values" => "#eeeeee;#d0d0d0","print" => false, "reset" => true", "delimiter" => ";"));But not only is that a lot more verbose and messy, but it provides nolanguage features in the function/method definition itself, so youjust have to hope the big array that comes in has the right stuff init. Not ideal.Anyway, I hope that helps, and if you have any other thoughts orquestions, please shoot away.Regards,

Jared

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#4 Nov. 30, 2005 23:27:33

Bart d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


Hi Jared,If probably don't understand named arguments correclty but couldn't youdo 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 solidtemplating language, and, in fact, they also greatly enhance codereadability for complex method calls of in-depth APIs. My experiencewith both Objective-C and Python has showed me the wonders and joys ofnamed arguments, and it is something I've desperately wanted in PHPfor ages. I'm sure I'm not alone in this. I've tried array constructs,multiple arguments with string-based names and fancy parsing usingfunc_get_args(), and various combinations thereof, and nothing is agood substitute for the real deal.--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#5 Nov. 30, 2005 23:30:43

Bart d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


Sorry... Forget I said that...

Bart de Boer wrote:Hi Jared,If probably don't understand named arguments correclty but couldn't youdo 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 solidtemplating language, and, in fact, they also greatly enhance codereadability for complex method calls of in-depth APIs. My experiencewith both Objective-C and Python has showed me the wonders and joysof named arguments, and it is something I've desperately wanted inPHP for ages. I'm sure I'm not alone in this. I've tried arrayconstructs, multiple arguments with string-based names and fancyparsing using func_get_args(), and various combinations thereof, andnothing is a good substitute for the real deal.--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#6 Dec. 1, 2005 07:46:05

Ron K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


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:

<?php
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:

<?php
function adduser($params)
{
if (!is_array($params)) throw new Exception('No named parameters');
if (!isset($params)) throw new Exception('Missing parameter:
username');
if (!isset($params)) throw new Exception('Missing parameter:
username');
if (!isset($params)) $params = 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

> 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.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#7 Dec. 1, 2005 14:59:58

Christian S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


Ron Korving wrote:Named parameter example:Your example misses the main advantage of named parameters IMHO: Sets ofparameters you don't want to or can't explicitely list because they arenot know yet.function adduser($params)
{
if (!is_array($params)) throw new Exception('No named parameters');if (!isset($params)) throw new Exception('Missing parameter:username');if (!isset($params)) throw new Exception('Missing parameter:username');if (!isset($params)) $params = 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 setof fixed parameters then positional parameters are working just fine.One of the main advantages lies in a catch-all parameter which gets allunassignable parameters. While this opens the door for typos even awhips'n'chains language like Python added this feature because it isjust too useful to omit if you have named parameters :-)Our use case is something along the lines of (not the actual code, sodon'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</$name>";
}

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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#8 Dec. 1, 2005 18:46:35

Jared W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


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 listbecause they are not know yet.function adduser($params)
{if (!is_array($params)) throw new Exception('No namedparameters');if (!isset($params)) throw new Exception('Missingparameter: username');if (!isset($params)) throw new Exception('Missingparameter: username');if (!isset($params)) $params = 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 afinite set of fixed parameters then positional parameters areworking just fine.Well, I wouldn't say that. The problem isn't that positionalparameters don't work fine -- they do -- but that upon codeinspection you don't know what those parameters are for unless youknow the function/method API ahead of time. Sure, as you pointed outthere are some juicy features you get with named parameters if youimplement them throughly, but I still say its main selling point isthat the calling code is its own API documentation.Regards,

Jared

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#9 Dec. 1, 2005 19:36:11

Jared W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


On Dec 1, 2005, at 11:16 AM, Sebastian Kugler wrote:On 12/1/05, Jared White <> wrote:I still say its main selling point is
that the calling code is its own API documentation.why don't you just write something like

cycle(/* name: */ "myCycle", /* values: */ "#eeeeee;#d0d0d0", /*
print: */ false,
/* reset: */ true, /* delimiter: */ ";");

It's just as self-explanatory and you don't have to change the
language for it :-)Hmm, that's not such a crazy idea. :)
Wait, hey, that's not fair! Now you've spoiled everything! :)Seriously though, it still doesn't address the fact that the function/method definition itself needs to have the named parameter notationto match, plus the idea of the catch-all parameters (fromfunc_get_args() presumably) having string keys (I think that's whatChristian mentioned which is a cool Python feature).Jared

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#10 Dec. 1, 2005 20:00:06

Sebastian K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Named arguments revisited


On 12/1/05, Jared White <> wrote:
> I still say its main selling point is
> that the calling code is its own API documentation.

why don't you just write something like

cycle(/* name: */ "myCycle", /* values: */ "#eeeeee;#d0d0d0", /*
print: */ false,
/* reset: */ true, /* delimiter: */ ";");

It's just as self-explanatory and you don't have to change the
language for it :-)

--Sebastian

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] Named arguments revisited [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support