note 65193 added to language.oop5.basic

From: Date: Wed, 26 Apr 2006 16:21:33 +0000
Subject: note 65193 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-109761@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 129.16.214.50 Probable Submitter: 85.30.174.70 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- http://master.php.net/note/edit/65193 Del: integrated -- http://master.php.net/note/delete/65193/integrated Del: useless -- http://master.php.net/note/delete/65193/useless Del: bad code -- http://master.php.net/note/delete/65193/bad+code Del: spam -- http://master.php.net/note/delete/65193/spam Del: non-english -- http://master.php.net/note/delete/65193/non-english Del: in docs -- http://master.php.net/note/delete/65193/in+docs Del: other reasons-- http://master.php.net/note/delete/65193 Reject -- http://master.php.net/note/reject/65193 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109761) next »