note 106342 deleted from language.references.unset by danbrown
| From: | danbrown@php.net | Date: | Sat, 29 Oct 2011 14:11:35 +0000 |
| Subject: | note 106342 deleted from language.references.unset by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184213@lists.php.net to get a copy of this message | ||
Note Submitter: Peter
----
The claim that unset does not work on objects does not appear to be correct to me (lowlight 20 Sep
11) as the following code shows:
$a = new stdclass();
$a->something = true;
unset($a);
var_dump($a); // $a is now undefined as one would expect.
I think that there is a problem with the code given in the previous post:
function test(&$trash)
{
unset($trash);
}
$a = new stdclass();
$a->something = true;
test($a);
var_dump($a); // $a is still defined (and unchanged).
My best understanding (guess?) of why the unset in the function call does not work is that the
'unset($trash)' line unbinds the $trash parameter from $a in much the same way that the
test_rebind function (below) would remove the binding between $trash and $a and replace it with a
binding to $GLOBALS and not cause a binding between $a and $GLOBALS.
function test_rebind(&$trash)
{
$trash = $GLOBALS;
}
An additional comment on the nuke_me function presented is that the object property
'something' is unset but the object '$a' is still defined which, I presume, was
not the intention of the code.