Re: Reassigning variable to new object destroys old one?
- From: ZeldorBlat <zeldorblat@xxxxxxxxx>
- Date: 30 Apr 2007 07:43:07 -0700
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>
.
- Follow-Ups:
- Re: Reassigning variable to new object destroys old one?
- From: Tyno Gendo
- Re: Reassigning variable to new object destroys old one?
- From: Toby A Inkster
- Re: Reassigning variable to new object destroys old one?
- References:
- Reassigning variable to new object destroys old one?
- From: woger151
- Reassigning variable to new object destroys old one?
- Prev by Date: Reassigning variable to new object destroys old one?
- Next by Date: Re: Programming PHP offline
- Previous by thread: Reassigning variable to new object destroys old one?
- Next by thread: Re: Reassigning variable to new object destroys old one?
- Index(es):