note 105845 added to language.references.unset
| From: | lowlight1974 at gmail dot com | Date: | Tue, 20 Sep 2011 00:28:34 +0000 |
| Subject: | note 105845 added to language.references.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183282@lists.php.net to get a copy of this message | ||
Unsetting array and objects does NOT work when trying to unset them. You have to iterate through
them in order to empty them out, akin to cleaning out memory in c/c++. Here are some examples:
<?php
echo "<pre>";
$a->something = true;
echo "Running test(\$a)\n";
test($a);
//At this point $a still has the something=true values
var_dump($a);
echo "\nRunning nuke_me(\$a)\n";
nuke_me($a);
//At this point, the values in $a have been deleted.
var_dump($a);
echo "</pre>";
//Now, one would think that $a would be unset after running test(). But in this instance, $a is
UNTOUCHED.
//Call $a by reference, name it something else for giggles
function test(&$trash)
{
unset($trash);
}
function nuke_me($var)
{
if (is_object($var) || is_array($var))
{
foreach ($var AS $key => $value)
{
if (isset($key))
{
if(is_object($var))
{
unset($var->$key);
}
if(is_array($var))
{
unset($var[$key]);
}
}
}
}
else
{
unset($var);
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 67.251.5.232
----
Manual Page -- http://www.php.net/manual/en/language.references.unset.php
Edit -- https://master.php.net/note/edit/105845
Del: integrated -- https://master.php.net/note/delete/105845/integrated
Del: useless -- https://master.php.net/note/delete/105845/useless
Del: bad code -- https://master.php.net/note/delete/105845/bad+code
Del: spam -- https://master.php.net/note/delete/105845/spam
Del: non-english -- https://master.php.net/note/delete/105845/non-english
Del: in docs -- https://master.php.net/note/delete/105845/in+docs
Del: other reasons-- https://master.php.net/note/delete/105845
Reject -- https://master.php.net/note/reject/105845
Search -- https://master.php.net/manage/user-notes.php