SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need access to central object throughout tree

    I am building an iCalendar library for PHP. The library stores "components" in a tree, each represented by a descendant of qCal_Component. Each has access to its children and to its parent. Each component also needs access to any other object in the tree at any given time. For instance, any component which makes reference to a time zone must be able to verify that the timezone exists in a "vtimezone" object somewhere in the tree. I suppose I could use a registry and store references to each object in the tree so that I could easily access any one I want, but I don't really like the idea of using a registry. I might as well store everything in $GLOBALS if i'm going to do that. Ideas?

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't that exactly what a registry is supposed to do, why would you want to avoid using a registry if it solves the very problem you are looking for a solution to?
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A registry just feels like a hack to me. It seems like storing a global registry throughout the tree is just bad design. There is no encapsulation. Plus, let's say we have two different qCal objects running side-by-side. Now we not only have one object storing data in a global registry, but TWO. Possibly completely unrelated. Components should know about eachother without having to resort to a registry.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do it kinda like the dom, where every node knows the root(or knows to just walk up the chain of parents to get to it). The root can serve as the "registry" for stuff like this that needs to have scope over it's entire tree. Since you already have it working so that they know about parent/child, you shouldn't really need to modify much code at all aside from possibly the way you create the root of the tree structure.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's exactly what I decided to do. Basically I added a getRootComponent() method to my base component class and now each component can get access to anything in the tree that way. Thanks man!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •