Re: Function parameter passed by reference with default value to null
| From: | David Otton | Date: | Mon, 06 Jul 2009 10:04:57 +0000 |
| Subject: | Re: Function parameter passed by reference with default value to null | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294940@lists.php.net to get a copy of this message | ||
2009/7/6 Lupus Michaelis <mickael+php@lupusmic.org>:
> 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.