On 20 January 2015 at 20:09, Marc Bennewitz <dev@mabe.berlin> wrote:
> Pros & Cons
>
> _Named Parameters:_
> pro:
>     - readability  (If caller is not required to call as assoc array)
>     - not an option for inheritance
> con:
>     - paramter skipping is a side product with possible edge cases
>     - Adds variable names to be part of the API
>     - implementation complexity


Nice summary, but I think "Adds variable names to be part of the API"
is such a big thing it deserves two lines, as it would be horrible for
things like anonymous functions or callbacks. e.g.

<?php
$f = function ($foo = 'first', $bar = 'second') {
    return $foo."_".$bar."\n";
};

$g = function ($bar = 'first' , $foo = 'second') {
    return $bar."_".$foo."\n";
};

if (rand(0,2)) {
    $h = $f;
}
else {
    $h = $g;
}

// Both output 'first_second'
echo $h('first', 'second');
echo $h('first');

// 'default' placeholder outputs 'first_second'
echo $h(default, 'second');


// Named param outputs 'second_second' some of the time.
echo $h(bar => 'second');

?>

Although named params sound like a nice idea, looking at how they
would act in a large code base, it seems like they would be a source
of unexpected issues, when people make assumptions about a function
based on the argument names. People would probably have to mark
libraries as safe to call via named params or not, which doesn't sound
good.

I'm pretty sure that parameter order is what actually defines a
function's signature and so the 'default' placeholder is the only
option that makes sense.

cheers
Dan

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

Reply via email to