Hello.

A suggestion I would like to make is to allow for nothing to be
supplied for defaulted parameters.

I suppose the easiest way of describing this issue is with the
following code ...

<?php
function foo($bar, $baz = 9, $buzz = 10) {
 return "$bar $baz $buzz";
}

// Whatever is supplied for $baz will be used for $baz.
// User has to know the default value of $baz rather than just
allowing the default value.
echo foo(1, 9, 20);

I don't know the stylics on using default parameters, but for the user
to have to know the default value would sort of make the default
redundant.


// Passing nothing at all could be one option.
echo foo(1, , 20);

but who would want to see ...

echo anotherfoo(1, , , , , , , 20);

for example.


Maybe a new keyword ... (ala SQL syntax).

echo foo(1, default, 20);


I know a solution could be ...

function foo($bar, $baz = 9, $buzz = 10) {
 $baz = is_null($baz) ? 9 : $baz;
 return "$bar $baz $buzz";
}

but, now, the default value is in 2 places and looks more complex.

More parameters would result in more tests for null.

Of course, null may be a valid value (it may be interpreted as zero or
empty string or false due to PHP's loose types).


If the user determines the default value and codes for it ...

echo foo(1, 9, 20);

but then the default value is changed, the code will output the same
value, but may not be the appropriate value.


The code ...

echo foo(1, , 20)
echo foo(1, default, 20)

does say, at least from the user's perspective, that the default value
must be used and doesn't require the user to know what the value is.


Regards,

Richard Quadling.


-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Reply via email to