By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,350 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,350 IT Pros & Developers. It's quick & easy.

Reassigning variable to new object destroys old one?

P: n/a
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

Apr 30 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Apr 30, 10:35 am, woger...@jqpx37.cotse.net 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....se_frm/thread/
7046181c4c507263/3e766edeeebbd5f5#3e766edeeebbd5f5>

Apr 30 '07 #2

P: n/a
ZeldorBlat wrote:
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....se_frm/thread/
7046181c4c507263/3e766edeeebbd5f5#3e766edeeebbd5f5>
At the risk of starting the $a++ thread again ... hehe

I believe that will be why $a = $a++; doesn't work.

The PHP manual should say $a++ "Returns value of $a" not "Returns $a..."
which to me infers it returns itself as a reference and the ++ should work.

But, returns value of a, hence $a overwrites itself, and the ++ tries to
take effect on the original which now has no references so doesn't exist.

Umm... Monday afternoon.. I'm bored and perplexed at the same time.

I don't think that helped you, sorry. I'll get my coat.
Apr 30 '07 #3

P: n/a
ZeldorBlat wrote:
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.
This isn't strictly true. There may still be other references to the
object floating about.

class Foo
{
public static $primary_instance = NULL;
public $val;

public function __construct ($n)
{
if (!isset(self::$primary_instance))
self::$primary_instance = $this;
$this->val = $n;
}

public function carp ()
{
echo $this->val . "\n";
}
}

$x = new Foo(123); // First Foo object

$x = new Foo(456); // Replacement Foo object
$x->carp();

// But the first one is still hanging around
// because another reference to it exists.
Foo::$primary_instance->carp();

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
Apr 30 '07 #4

P: n/a
On Apr 30, 12:40 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
ZeldorBlat wrote:
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.

This isn't strictly true. There may still be other references to the
object floating about.

class Foo
{
public static $primary_instance = NULL;
public $val;

public function __construct ($n)
{
if (!isset(self::$primary_instance))
self::$primary_instance = $this;
$this->val = $n;
}

public function carp ()
{
echo $this->val . "\n";
}

}

$x = new Foo(123); // First Foo object

$x = new Foo(456); // Replacement Foo object
$x->carp();

// But the first one is still hanging around
// because another reference to it exists.
Foo::$primary_instance->carp();

--
Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
Right -- so, like I said, the object is destroyed when nothing is
referencing it.

Apr 30 '07 #5

P: n/a
ZeldorBlat wrote:
Right -- so, like I said, the object is destroyed when nothing is
referencing it.
But you also said:
When you assign the second instance to $x, there is nothing left
referencing the first instance.
Which is not necessarily correct.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
Apr 30 '07 #6

P: n/a
On Apr 30, 10:43 am, ZeldorBlat <zeldorb...@gmail.comwrote:
On Apr 30, 10:35 am, woger...@jqpx37.cotse.net 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....se_frm/thread/
7046181c4c507263/3e766edeeebbd5f5#3e766edeeebbd5f5>- Hide quoted text -

- Show quoted text -
Great---thanks for the informative reply.

May 1 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.