Function parameter passed by reference with default value to null

View: New views
8 Messages — Rating Filter:   Alert me  

Function parameter passed by reference with default value to null

by Lupus Michaelis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   Hi,

   I would like to know if I am alone to be shoked by this :

== 8< ==
function foo(& $bar = null)
{
}

foo() ; // runs
foo(null) ; // raise an error
== >8 ==

   Why the default value to null for a reference is allowed ? Is it a
bug, a feature ?

   Thanks !
--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Seeking for a position <http://lupusmic.org/pro/>

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


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

by Stuart-47 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

>  Hi,
>
>  I would like to know if I am alone to be shoked by this :
>
> == 8< ==
> function foo(& $bar = null)
> {
> }
>
> foo() ; // runs
> foo(null) ; // raise an error
> == >8 ==
>
>  Why the default value to null for a reference is allowed ? Is it a bug, a
> feature ?

You appear to be using the definition of null that comes from the
world of C. A null in PHP is not the same as a null in C - it's a real
value (of type null), not the absence of a value. So what you're doing
is the equivalent of passing a literal value, something you can't do
when the function is expecting a reference.

See the manual for more info on the null type: http://php.net/null

-Stuart

--
http://stut.net/

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


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

by Lupus Michaelis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stuart a écrit :

> You appear to be using the definition of null that comes from the
> world of C.

   I didn't. The point is I'm allowed to set a default value to null for
a referenced parameter (what I can do in C for a pointer, so I knew it).

   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 ?

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Seeking for a position <http://lupusmic.org/pro/>

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


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

by Stuart-47 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

> Stuart a écrit :
>
>> You appear to be using the definition of null that comes from the
>> world of C.
>
>  I didn't. The point is I'm allowed to set a default value to null for a
> referenced parameter (what I can do in C for a pointer, so I knew it).
>
>  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 ?

The whole point of default arguments is for it to use that argument if
none is passed.

What makes you think not passing an argument to that function should
raise an error?

-Stuart

--
http://stut.net/

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


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

by Lupus Michaelis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stuart a écrit :

> The whole point of default arguments is for it to use that argument if
> none is passed.
   It is not the point too.

> What makes you think not passing an argument to that function should
> raise an error?
   Maybe because in my example, the provided value is not a valuable
value for a reference ?

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Seeking for a position <http://lupusmic.org/pro/>

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


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

by David Otton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

>  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


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

by Lupus Michaelis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

David Otton a écrit :

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

   Thanks for this smart explanation. It shines my day.

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Seeking for a position <http://lupusmic.org/pro/>

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


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

by David Otton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/6 Lupus Michaelis <mickael+php@...>:
>
> David Otton a écrit :
>
>> 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:
>
>  Thanks for this smart explanation. It shines my day.

np. On reflection, it seems to me that if they were dead-set on
default values for pass-by-reference variables, they should have
dropped the restriction on passing literals by reference at the same
time, and just thrown away the result as happens with default values.
Oh well, it's not my language design.

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