note 65193 deleted from language.oop5.basic by bjori
| From: | bjori@php.net | Date: | Wed, 26 Apr 2006 16:47:45 +0000 |
| Subject: | note 65193 deleted from language.oop5.basic by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109766@lists.php.net to get a copy of this message | ||
Note Submitter: Andreas Blixt
Reason: in docs
----
In response to ksam at mail dot ru (and as further, more technical clarification to the explanation
below this note):
<?php
header("Content-type: text/plain");
class myClass {
public $value;
function __destruct() { echo chr(10) . "* The garbage collector got me (\$class" .
$this->value . ")! *" . chr(10); }
}
$classA = new myClass(); $classA->value = 1;
var_dump($classA); // $classA->value shows as 1.
$classB = $classA; // There is still only one instance of myClass after this.
$classB->value = 2;
var_dump($classA); // $classA->value will now show a value of 2.
$classRef = &$classA; $classRef->value = "A";
var_dump($classA); // $classA->value will now show a value of "A".
// You might expect the following to destroy the class instance, but it won't.
// If $classB hadn't been set to point to the class, it actually WOULD have
// been unset, because the garbage collector would destroy it.
unset($classA); unset($classRef);
echo chr(10) . "classA: "; var_dump($classA); // $classA is now NULL.
echo "classB: "; var_dump($classB); // $classB is still showing the same old
class instance.
$classC = new myClass(); $classC->value = "C";
echo chr(10) . "classC: "; var_dump($classC); // Notice how it shows #2 after (myClass).
That's the index
// of this new instance of myClass, and that's
basically what
// $classC now contains, not the actual instance of
the class.
$classB = NULL; // The destructor for what was originally $classA (and until now, $classB) triggers
// immediately after this statement. Not because $classB which held the class
address
// was nullified, but because there is no longer any variable pointing to the class
// instance, which means it's virtually irretrievable from within the script.
echo chr(10) . "----> Script ends here. <----" . chr(10);
?>