note 51396 added to language.oop5.basic
| From: | graced at monroe dot NOSPAM dot wednet dot edu | Date: | Tue, 29 Mar 2005 18:46:38 +0000 |
| Subject: | note 51396 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87157@lists.php.net to get a copy of this message | ||
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.
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51396
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51396&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51396&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51396&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51396&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51396&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51396&report=yes
Search -- http://master.php.net/manage/user-notes.php