Is it safe to have circular references in PHP objects?
Moderator: General Moderators
Is it safe to have circular references in PHP objects?
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?
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is it safe to have circular references in PHP objects?
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:Any dangers of memory leak?
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.skruby wrote:How it works is like this:
echo p(' hello WorLD wOO')->trim()->capitalize_words()->chomp(); // Output: Hello World Wo
Wow! When feyd fully commits to a position -- Katy bar the door!feyd wrote:It's usually okay for the most part.
(#10850)
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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?
Oh. The reason i say it's circular reference is this.arborint wrote: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:Any dangers of memory leak?
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.skruby wrote:How it works is like this:
echo p(' hello WorLD wOO')->trim()->capitalize_words()->chomp(); // Output: Hello World WoWow! When feyd fully commits to a position -- Katy bar the door!feyd wrote:It's usually okay for the most part.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is it safe to have circular references in PHP objects?
Well ... when you say it that way is just sounds plain crazy!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.
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.
(#10850)
Re: Is it safe to have circular references in PHP objects?
You should consider using decorator pattern. having trim() and capitalize() is all you need to impelment trim and capitalize at the same time.skruby wrote:........
Module A has the function trim() and capitalize();
Module B has the function trim_and_capitalize();
......
Introducing another function that combines both and the code starts to smell no so good.