Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
Something I was doing yesterday got me onto thinking about patterns once again.
I wrote some code that basically creates an recurisve object structure whereby the parent holds an instance of a child, which itself holds an instance of the parent.
It looked a bit nasty on first sight but after doing some research it appears that this is actually fairly common practice in other languages such as Java and C# (I even saw a few Perl implementations).
A few articles I read mentioned that the "composite pattern" is brilliant when working with such structures but the most info I've managed to find out so far is this carppy little diagram:
In JAVA, we were always taught that a composite pattern was this, it looks and sounds slightly different to what you thought it is. Not sure who is right
someberry wrote:In JAVA, we were always taught that a composite pattern was this, it looks and sounds slightly different to what you thought it is. Not sure who is right
I guess your version is right... the only thing I was going on was the brief passing of that diagram. I haven't got a clue what exactly I'm looking at there Does that actually even relate to a recursive object structure?
Ever feel like you're about 5% as clever as you thought you were?
d11wtq wrote:I guess your version is right... the only thing I was going on was the brief passing of that diagram. I haven't got a clue what exactly I'm looking at there Does that actually even relate to a recursive object structure?
Ever feel like you're about 5% as clever as you thought you were?
Yeah, the Composite/Component pattern is used for tree structures. It combines a few other patterns, but the trick seems to be that the parent knows just enough about the child nodes to make a useful linkage. As you probably guess the composite and component are often the same class that can be both a parent and a child.
The problem I had with the code in your original post (and you mailer auth classes ) is that the dependency goes the other direction. It can do that if it really has to, but if it doesn't then cleaner dependencies with make things more maintainable.
Sounds like what you've designed is an ADT (sorta)
A container class, like those found in STL (although tree isn't included it's part of the same kind of family)...
Here is my latest implementation which is part of my own whittle framework - although not entirely tested or complete it will eventually become the base class for my own DOM level 3 implementation...
/*
NOTES:
We need this global because PHP does not support static data members and it's object
comparison '===' is not sufficient. PHP compares objects by the following method:
1) Both objects have the same attributes and values.
2) Both objects are of the same type
The problem with the above, is there are instances where two objects might have
the exact properties and values, but be instantiated in seperately, in which case
the only kind of object comparison that will work is instance comparison.
*/
$g_swiftObjectInstance_8934JKDF87923JHYUGF789; // Initialize to zero
//
// Interface and Implementation
//
class CxSwiftBase{
function addInstance()
{
global $g_swiftObjectInstance_8934JKDF87923JHYUGF789;
$this->m_objInstance = (int)$g_swiftObjectInstance_8934JKDF87923JHYUGF789++;
}
/* protected */ var $m_objInstance;
}