Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't pass $this by reference

Status
Not open for further replies.

Algernon83

Programmer
Jun 25, 2004
50
US
Short version: Why does setting $myobject->myattribute = &$this from inside of another object's constructor pass a copy of $this? How can I pass $this by reference?

Long version:

The source in question can be found at
I have a class called CollapsibleCategory that encapsulates a <div> and an image that, when clicked, makes it appear and disappear. To make this happen, it includes a little delegate that I call an ElementHider. The ElementHider takes in an element to show or hide and one to be clicked to show or hide it. This is arranged in the CollapsibleCategory's constructor, like so:

$this->Hider->InterfaceElement = &$this->Interface;
$this->Hider->HiddenElement = &$this;

So the ElementHider ($this->Hider) should get the ID attribute of $this->Interface and $this when rendering in order to display its JavaScript, but it only works for $this->Interface.

After the CollapsibleCategory is constructed and makes its ElementHider, I call SetID on the CollapsibleCategory, which changes the ID of its interface element. This is rendered properly from the ElementHider, but it does not recognize that the ID of the CollapsibleCategory has changed. If I repeat:
$this->Hider->HiddenElement = &$this;
After changing $this->id, however, it renders properly. Clearly, it's passing by copy. Why?
 
Thank you. That didn't answer my question directly (in fact it doesn't specifically mention $this at all) but it does mention that PHP5 handles = differently for objects than does PHP4. In fact it handles it exactly as I wanted, and upgrading solved my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top