Re: Re: unsetting a referenced parameter in a function

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

 



On Wed, Jul 22, 2009 at 9:27 PM, Tom Worster <fsb@xxxxxxxxxx> wrote:

> On 7/22/09 6:09 PM, "Shawn McKenzie" <nospam@xxxxxxxxxxxxx> wrote:
>
> > Tom Worster wrote:
> >> though the manual is perfectly clear that this should be expected, i was
> a
> >> bit surprised that the result of the following is 42
> >>
> >> <?php
> >> function foo(&$a) {
> >>   $a = 42;
> >>   unset($a);
> >>   $a = 'meaning';
> >> }
> >> foo($a);
> >> print("$a\n");
> >> ?>
> >>
> >> normally i would expect unset() to free some memory. but in this example
> it
> >> doesn't and has a different behavior: it releases foo's reference to the
> >> global $a, allowing the next line to define a local $a.
> >>
> >> i think i'd have preferred compile error.
> >>
> >>
> >
> > Well, you unset the reference and then you assigned 'meaning' to a local
> > function variable $a.  Why would you get a compile error?
>
> when you state it in those terms (which are clearly correct) i wouldn't.
>
> but if the way i think is "unset() destroys the specified variables" (as
> the
> manual puts it) then i expect that the specified variable would be
> destroyed, not the reference.
>
> so, as i said, i was a bit surprised when the variable wasn't destroyed.
> once i understood what was happening, i thought it a bit confusing to have
> such scope-dependent differences in behavior of a language element.
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
RTM: http://php.net/unset

The behavior of *unset()* inside of a function can vary depending on what
type of variable you are attempting to destroy.

If a globalized variable is *unset()* inside of a function, only the local
variable is destroyed. The variable in the calling environment will retain
the same value as before *unset()* was called.

/ ... /

If a variable that is PASSED BY REFERENCE is *unset()* inside of a function,
only the local variable is destroyed. The variable in the calling
environment will retain the same value as before *unset()* was called.


I think the manual is very clear about unset.

-- 
Martin Scotta

[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