473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reference to THIS from inside constructor

Hi there,
I heard storing reference to THIS from inside object's constructor can
cause problems:

$myObjects = array();
class MyClass{
function MyClass(){
global $myObjects;
$myObjects[] = $this;
}
}

Do you know anything about that? It looks like when constructor
finishes all such references lead to COPY of the object.

Feb 1 '07 #1
1 1173
Tom
On Feb 1, 9:55 am, "Tamagafk" <tamag...@gmail.comwrote:
Hi there,
I heard storing reference to THIS from inside object's constructor can
cause problems:

$myObjects = array();
class MyClass{
function MyClass(){
global $myObjects;
$myObjects[] = $this;
}

}

Do you know anything about that? It looks like when constructor
finishes all such references lead to COPY of the object.
If you actually want to reference instead of copy, you need to do:
$myObjects[] = &$this ;

You can also do this outside the constructor when defining your
instance, e.g.:
$myObjects[] = &$ref ;
$ref = new MyClass() ;

Feb 1 '07 #2

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

Similar topics

4
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in...
5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
3
by: marco_segurini | last post by:
Hi, the 'user' class describes what is my target: I want to save a reference to a class instance and a reference to the reference to the class instance calling a 'user' member function and...
3
by: Mochuelo | last post by:
Hi, In the constructor of a class, I am taking as a parameter the reference to a "bool", as in this code: ----------------------- public class TCP_synch_client { public string address...
5
by: mikaelhc | last post by:
Playing a bit around with Trolltech's Qt library I noticed the following constructor: QProgressDialog ( const QString & labelText, const QString & cancelButtonText, ...) In the documentation...
8
by: utab | last post by:
Dear all, I am getting an undefined reference error wrn I try to initilialize a static map in the class constructor. my map: map<string,vector<string> > m; and in the constructor ...
0
by: Dan | last post by:
Hi!, When I Pass my Client Class Between Form1 to Form2 to Form3 I lose reference If inside the Form3 I Reset the Client With "New Client" Example : public class Client
0
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.