Re: $this->this and avoiding circular references
| From: | Jochem Maas | Date: | Wed, 07 Dec 2005 16:12:11 +0000 |
| Subject: | Re: $this->this and avoiding circular references | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-21079@lists.php.net to get a copy of this message | ||
are you using ph4 or php5?
are you setting $this->this yourself or 'is it just there'?
Alan Pinstein wrote:
Hi all- I've been trying to avoid circular references in some data import scripts and finally figured out how to do it. However, I wanted to ask you guys to make sure that what I'm doing is something that's legit and can be relied on into the future. Also, please note that I tried searching the archives for "this" and "this->this" but the searcher keeps removing "this" from my query! So sorry if this is asked and answered.... So, my objects are in a parent-child relationship and they both have links to each other... pretty standard circular reference scenario:assuming php4, I would say this these lines should be (difficult to tell though, without more insight/code/brains to play with ;-): function addChildObject(&$c) {ParentObject: function addChildObject($c) {$this->children[] = $c; $c->setParent($this->this); // this syntax is the only way
$this->children[] =& $c;
$c->setParent( $this );
}
I found to make "$this" not get refcounted here } Now, to make this work as it should without incrementing the ref counts when storing the link to the parent, setParent() needs to have a non-refcounted pointer to the object: ChildObject: // need & in both places to make the ref not get ref-counted function setParent(&$parent) { $this->-parent = &$parent; }My question is, is "$this->this" valid syntax? Can it be relied on? Is my overall approach sane? It's very important that I get memory management working in such a way here because the objects are used in large loops for importing data, and without this, the script uses unbounded amounts of memory. With this, it uses a finite amount of memory and works beautifully. Thanks in advance, Alan