Re: Reassigning variable to new object destroys old one?



On Apr 30, 10:35 am, woger...@xxxxxxxxxxxxxxxx wrote:
If I write
$x = new some_class($init_data1);
and then
$x = new some_class($init_data2);

does the first object (constructed with $init_data1) get destroyed, or
do I have to call unset() for that? And am I guaranteed that after
the second call the value of $x will be as if the first call was never
made?

I tried using var_dump within a for loop to see what was happening
(ie, using "new" and assigning it to the same variable name everyt
ime), and in the printout I get stuff like
object(some_class)#1
alternating with
object(some_class)#2

If I call unset(), I don't get the #2. I'm assuming that (in the case
I don't call unset()) PHP is destroying the variable every other
iteration due to some kind of garbage collection algorithm. And the
"#1" and "#2" refer to the actual object being referenced.

TIA

The object is automatically destroyed when nothing is referencing it.
When you assign the second instance to $x, there is nothing left
referencing the first instance.

There was another thread on this subject recently:
<http://groups.google.com/group/comp.lang.php/browse_frm/thread/
7046181c4c507263/3e766edeeebbd5f5#3e766edeeebbd5f5>

.