Page 1 of 1
Is it safe to have circular references in PHP objects?
Posted: Thu Nov 23, 2006 3:43 pm
by skruby
Any dangers of memory leak? I've used circular references extensively in my latest project (See
viewtopic.php?p=332580#332580)
How it works is like this:
echo p(' hello WorLD wOO')->trim()->capitalize_words()->chomp(); // Output: Hello World Wo
The framework structure is as follows
Pruby_Engine refers to many Pruby_Modules (one to many relationship)
Each module refer to Pruby_Engine (one to one relationship)
So as you can see, there is a circular reference between the Pruby Engine object and each Pruby Module.
I'm using the Engine as a proxy so for eg.
Module A has the function trim() and capitalize();
Module B has the function trim_and_capitalize();
In Module B: function trim_and_capitalize() { $this->trim(); $this->capitalize(); }
Because there are no trim and capitalize functions in the Module B class, doing so will call the magic __call function of the module which will then forward the trim() and capitalize() request to the Engine, which will then forward them to module A. Thus, the access path is like this: Module B -> Engine -> Module A
I've not encountered any prob so far but is this approach scalable? Will it wreak havoc once i have 100 modules?
Posted: Thu Nov 23, 2006 4:07 pm
by feyd
It's usually okay for the most part.
Re: Is it safe to have circular references in PHP objects?
Posted: Thu Nov 23, 2006 4:20 pm
by Christopher
skruby wrote:Any dangers of memory leak?
There is little to no danger of a memory leak in PHP due to the built-ing garbage collection and short duration of scripts.
skruby wrote:How it works is like this:
echo p(' hello WorLD wOO')->trim()->capitalize_words()->chomp(); // Output: Hello World Wo
That's not a circular reference, per se, but is usually refered to as a "Fluent Interface" which in PHP means that methods return a handle to the object so that you can do chaining like you have shown. It is most common with Setters as you have done.
feyd wrote:It's usually okay for the most part.
Wow! When feyd fully commits to a position -- Katy bar the door!

Posted: Thu Nov 23, 2006 4:42 pm
by feyd
There wasn't much to elaborate on, so I saw no reason to say anything further.

Posted: Thu Nov 23, 2006 8:29 pm
by Ambush Commander
Hmm... when is it not okay?
Posted: Thu Nov 23, 2006 8:43 pm
by feyd
In PHP, I haven't seen a situation where it was hurting things (outside of maintenance), although I certainly consider it a potentially bad practice. It stems of experience with other languages where we are responsible for memory management. Since memory is handled entirely by PHP, most things are perfectly fine.
Re: Is it safe to have circular references in PHP objects?
Posted: Fri Nov 24, 2006 12:31 am
by skruby
arborint wrote:skruby wrote:Any dangers of memory leak?
There is little to no danger of a memory leak in PHP due to the built-ing garbage collection and short duration of scripts.
skruby wrote:How it works is like this:
echo p(' hello WorLD wOO')->trim()->capitalize_words()->chomp(); // Output: Hello World Wo
That's not a circular reference, per se, but is usually refered to as a "Fluent Interface" which in PHP means that methods return a handle to the object so that you can do chaining like you have shown. It is most common with Setters as you have done.
feyd wrote:It's usually okay for the most part.
Wow! When feyd fully commits to a position -- Katy bar the door!

Oh. The reason i say it's circular reference is this.
p() returns Pruby Engine object $engine. $engine->visitor has the value ' hellow WorLd wOO';
There is no trim function in $engine. $engine->trim() will forward the function call to a module which has the trim function.
in the trim function, the code is like so: function trim() { $this->engine->visitor = trim($this->engine->visitor); }
The module $mod has a reference to $engine via $mod->engine.
THe engine $engine has a reference to $mod via $this->mod
So, when you called trim(), you can actually passing the call request back and forth.
Access is like: $engine -> $mod -> $engine;
As you can see, there is a circular reference between $mod and $engine.
Re: Is it safe to have circular references in PHP objects?
Posted: Fri Nov 24, 2006 8:46 pm
by Christopher
skruby wrote:The module $mod has a reference to $engine via $mod->engine.
THe engine $engine has a reference to $mod via $this->mod
So, when you called trim(), you can actually passing the call request back and forth.
Access is like: $engine -> $mod -> $engine;
As you can see, there is a circular reference between $mod and $engine.
Well ... when you say it that way is just sounds plain crazy!
Seriously, I really don't think there is much of a problem or danger with your implementation -- as long as it work (an hopefully is testable) then your fine. But I certainly get the impression that your design may be wonky.
Re: Is it safe to have circular references in PHP objects?
Posted: Sat Nov 25, 2006 5:24 am
by jmut
skruby wrote:........
Module A has the function trim() and capitalize();
Module B has the function trim_and_capitalize();
......
You should consider using decorator pattern. having trim() and capitalize() is all you need to impelment trim and capitalize at the same time.
Introducing another function that combines both and the code starts to smell no so good.