note 70852 added to language.references
| From: | sneskid at hotmail dot com | Date: | Wed, 01 Nov 2006 05:33:24 +0000 |
| Subject: | note 70852 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119280@lists.php.net to get a copy of this message | ||
in addition to what 'jw at jwscripts dot com' wrote about unset; it can also be used to
"detach" the variable alias so that it may work on a unique piece of memory again.
here's an example
<?php
define('NL', "\r\n");
$v1 = 'shared';
$v2 = &$v1;
$v3 = &$v2;
$v4 = &$v3;
echo 'before:'.NL;
echo 'v1=' . $v1 . NL;
echo 'v2=' . $v2 . NL;
echo 'v3=' . $v3 . NL;
echo 'v4=' . $v4 . NL;
// detach messy
$detach = $v1;
unset($v1);
$v1 = $detach;
// detach pretty, but slower
eval(detach('$v2'));
$v1 .= '?';
$v2 .= ' no more';
$v3 .= ' sti';
$v4 .= 'll';
echo NL.'after:'.NL;
echo 'v1=' . $v1 . NL;
echo 'v2=' . $v2 . NL;
echo 'v3=' . $v3 . NL;
echo 'v4=' . $v4 . NL;
function detach($v) {
$e = '$detach = ' . $v . ';';
$e .= 'unset('.$v.');';
$e .= $v . ' = $detach;';
return $e;
}
?>
output {
before:
v1=shared
v2=shared
v3=shared
v4=shared
after:
v1=shared?
v2=shared no more
v3=shared still
v4=shared still
}
http://www.obdev.at/developers/articles/00002.html
says there's no such thing as an "object reference" in PHP, but with detaching it
becomes possible.
Hopefully detach, or something like it, will become a language construct in the future.
----
Server IP: 66.163.161.117
Probable Submitter: 74.118.124.47
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/70852
Del: integrated -- https://master.php.net/note/delete/70852/integrated
Del: useless -- https://master.php.net/note/delete/70852/useless
Del: bad code -- https://master.php.net/note/delete/70852/bad+code
Del: spam -- https://master.php.net/note/delete/70852/spam
Del: non-english -- https://master.php.net/note/delete/70852/non-english
Del: in docs -- https://master.php.net/note/delete/70852/in+docs
Del: other reasons-- https://master.php.net/note/delete/70852
Reject -- https://master.php.net/note/reject/70852
Search -- https://master.php.net/manage/user-notes.php