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

Reference to THIS from inside constructor

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.