note 95522 added to language.oop5.references
| From: | miklcct at gmail dot com | Date: | Thu, 07 Jan 2010 09:34:41 +0000 |
| Subject: | note 95522 added to language.oop5.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164727@lists.php.net to get a copy of this message | ||
Notes on reference:
A reference is not a pointer. However, an object handle IS a pointer. Example:
<?php
class Foo {
private static $used;
private $id;
public function __construct() {
$id = $used++;
}
public function __clone() {
$id = $used++;
}
}
$a = new Foo; // $a is a pointer pointing to Foo object 0
$b = $a; // $b is a pointer pointing to Foo object 0, however, $b is a copy of $a
$c = &$a; // $c and $a are now references of a pointer pointing to Foo object 0
$a = new Foo; // $a and $c are now references of a pointer pointing to Foo object 1, $b is still a
pointer pointing to Foo object 0
unset($a); // A reference with reference count 1 is automatically converted back to a value. Now $c
is a pointer to Foo object 1
$a = &$b; // $a and $b are now references of a pointer pointing to Foo object 0
$a = NULL; // $a and $b now become a reference to NULL. Foo object 0 can be garbage collected now
unset($b); // $b no longer exists and $a is now NULL
$a = clone $c; // $a is now a pointer to Foo object 2, $c remains a pointer to Foo object 1
unset($c); // Foo object 1 can be garbage collected now.
$c = $a; // $c and $a are pointers pointing to Foo object 2
unset($a); // Foo object 2 is still pointed by $c
$a = &$c; // Foo object 2 has 1 pointers pointing to it only, that pointer has 2 references: $a
and $c;
const ABC = TRUE;
if(ABC) {
$a = NULL; // Foo object 2 can be garbage collected now because $a and $c are now a reference to
the same NULL value
} else {
unset($a); // Foo object 2 is still pointed to $c
}
----
Server IP: 69.147.83.197
Probable Submitter: 119.247.149.171
----
Manual Page -- http://www.php.net/manual/en/language.oop5.references.php
Edit -- https://master.php.net/note/edit/95522
Del: integrated -- https://master.php.net/note/delete/95522/integrated
Del: useless -- https://master.php.net/note/delete/95522/useless
Del: bad code -- https://master.php.net/note/delete/95522/bad+code
Del: spam -- https://master.php.net/note/delete/95522/spam
Del: non-english -- https://master.php.net/note/delete/95522/non-english
Del: in docs -- https://master.php.net/note/delete/95522/in+docs
Del: other reasons-- https://master.php.net/note/delete/95522
Reject -- https://master.php.net/note/reject/95522
Search -- https://master.php.net/manage/user-notes.php