473,785 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reassigning variable to new object destroys old one?

If I write
$x = new some_class($ini t_data1);
and then
$x = new some_class($ini t_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_cla ss)#1
alternating with
object(some_cla ss)#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
6 2098
On Apr 30, 10:35 am, woger...@jqpx37 .cotse.net wrote:
If I write
$x = new some_class($ini t_data1);
and then
$x = new some_class($ini t_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_cla ss)#1
alternating with
object(some_cla ss)#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/
7046181c4c50726 3/3e766edeeebbd5f 5#3e766edeeebbd 5f5>

Apr 30 '07 #2
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/
7046181c4c50726 3/3e766edeeebbd5f 5#3e766edeeebbd 5f5>
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
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_instan ce = NULL;
public $val;

public function __construct ($n)
{
if (!isset(self::$ primary_instanc e))
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_i nstance->carp();

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
Apr 30 '07 #4
On Apr 30, 12:40 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
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_instan ce = NULL;
public $val;

public function __construct ($n)
{
if (!isset(self::$ primary_instanc e))
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_i nstance->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
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
On Apr 30, 10:43 am, ZeldorBlat <zeldorb...@gma il.comwrote:
On Apr 30, 10:35 am, woger...@jqpx37 .cotse.net wrote:


If I write
$x = new some_class($ini t_data1);
and then
$x = new some_class($ini t_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_cla ss)#1
alternating with
object(some_cla ss)#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/
7046181c4c50726 3/3e766edeeebbd5f 5#3e766edeeebbd 5f5>- Hide quoted text -

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

May 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4349
by: MS | last post by:
Hi, In my Genetic Algorithm program, I have a class called Genome. Another class called GA has a class variable called 'population' which is an array of type Genome. One of the methods in GA, called CreateNextGeneration(), creates a local array of class Genome called 'nextGeneration', and at the end of that method I want the class variable 'population' to hold the array 'nextGeneration'.
9
2048
by: Ken Godee | last post by:
I'm using the python Queue module, but since I can not find anyway to clear the Queue, I thought I would try to reassign ie...... q1 = Queue.Queue() q1.put('test1') q1.put('test2') q1.qsize() 2 q1 = Queue.Queue()
2
1993
by: Joost Ronkes Agerbeek | last post by:
Is it legal to reassign an auto_ptr to another auto_ptr that is a member of the object the auto_ptr is holding before the reassignment? (Confused yet? :-p) #include <memory> class Node { public: Node() : next(0) {} Node(Node* node) : next(node) {}
2
3355
by: Zach | last post by:
How do I call an object/element/property when one or more of the names are defined by a variable? I want to do something like this: .document.getElementById("").style.foo="bar" Am I missing something obvious, or is this not possible? And if not, any suggested work-arounds? Thanks!
2
1317
by: Geoff Cox | last post by:
Hello, I read that moving from one page to another normally destroys a variable. The code below allows the variable "name" to be used in a form on the second page. How does this happen? Or am I wrong? Thanks Geoff
4
5398
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
2
1899
by: Peter | last post by:
I have a program that uses multiple arrays and the memory useage is huge. After a proceedure ends is there a good way to shrink down an array's size. The same for variables.
3
1325
by: biswaranjan.rath | last post by:
Can i do something like this: <xsl:variable name="varMaxDiffId"> <xsl:choose> <xsl:when test="$varMaxDiffId == 'NULL'"> <xsl:value-of select="-1"/> </xsl:when> <xsl:when test="$varMaxDiffId < @unique_diff_id"> <xsl:value-of select="@unique_diff_id"/> </xsl:when>
8
1838
by: silversurfer2025 | last post by:
Hello everybody, I had this problem several times now and did not yet get the reasoning behind it. I have a class with a pointer as member variable, lets say a float array. Furthermore, I have two constructors of which the first is calling the second one... Here an easy example (so don't worry about the semantics ;) ) Member variables: float* pFeatVec;
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.