note 51396 deleted from language.oop5.basic by colder
| From: | colder@php.net | Date: | Sun, 19 Aug 2007 23:40:29 +0000 |
| Subject: | note 51396 deleted from language.oop5.basic by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129609@lists.php.net to get a copy of this message | ||
Note Submitter: graced at monroe dot NOSPAM dot wednet dot edu
----
In reponse to jerry's comment: "If you instantiate a class and assign it to a variable
over and over again, php will start saving instances #1 and #2 in memory but will stop at
these"...
This is because after assigning the new object to the variable, there is no longer anything
referencing the old object so it is destroyed. If there WAS another reference, say you assigned
another variable to the first variable before setting the first to a new class, it'd be
preserved.
In reality, this really makes no difference to the programmer, just thought an explanation was in
order.
<?php
$obj1 = new stdclass();
var_dump($obj1); // object #1
$obj1 = new stdclass();
var_dump($obj1); // object #2
// no more references to object #1, so it is destroyed.
$obj1 = new stdclass(); // object #1
var_dump($obj1);
$obj2 = $obj1; // two variables now reference object #1
$obj1 = new stdclass(); // becomes object #3
// no more references to object #2, so it is destroyed
var_dump($obj1);
$obj1 = new stdclass(); // object #2
//etc.
?>