Re: Function parameter passed by reference with default value to null

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/7/6 Lupus Michaelis <mickael+php@xxxxxxxxxxxx>:

>  I'm happy PHP raises an error on foo(null) ;
>  I'm in trouble when foo() doesn't.
>
>  The actual question is : why PHP doesn't raise an error ?

This functionality (default values for passed-by-reference parameters)
was added in PHP5.

The problem is that you can't pass literals by reference (which makes sense):

function f(&$a) {}
f(45); // error

But default values must be literals (which also makes sense):

function f($a = $_POST) {} // error

So there's some serious impedance mismatch going on there to make both
features to work together. Just think of the default value as
"something I can overwrite", eg:

function f(&$a = 45) { $a = 99; }

So it doesn't really matter if it starts off as 45, 'Hello World' or
null... it's going to get thrown away at the end of the function's
lifetime, anyway.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux