Results 1 to 4 of 4

Thread: Accessing a child class from a fellow child class?

  1. #1
    Join Date
    Jun 2006
    Under your bed

    Accessing a child class from a fellow child class?

    I have a parent class called sitewide and all my other classes are children of this. Sitewide has a bunch of stuff in it, and I'd like to organize it. I was thinking that, to do so, I'd group the things sitewide does together (e.g. database stuff, form checking stuff, etc.) and make an individual class for each category. So we'd have a database class, a form checking class, a layout class, etc. These would all be children of sitewide.

    Now here's the thing. In my other child classes of sitewide (e.g. login, signup, forum, etc.) how would they access the layout stuff, form checking stuff, etc., now that they're in fellow child classes?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  2. #2
    Join Date
    Jul 2003
    The City of Roses
    Actually, I think you're going to run into a lot of trouble with the architecture you just described. The classes you named most likely should not be descendants of sitewide. Instead, they should be independent components that sitewide makes use of.

    This article may give you some architecture ideas: From flat PHP to Symfony2.

  3. #3
    Join Date
    Apr 2010
    There are reasons to use parent child.

    I prefer to think of and just use the parent as the basic template with the child as the exact type of class needed.

    If one child OBJECT needs data from another use getter methods.
    Same goes for GETTERS.

    protect the properties
    public the setter getter methods.

  4. #4
    Join Date
    Aug 2004
    Inheritance normally should pass the "is a" test. If your Database object is a type of Sitewide object, then it makes sense to use inheritance. Based on those names, however, I'm guessing that is not the case; in which case I suspect that your Database object probably should use the Sitewide object, perhaps passed to or instantiated by the Database class's constructor. (Passing it in makes that dependency more obvious, so I generally prefer that over having the class instantiate it itself -- but it's not a hard and fast rule.)

    PHP Code:
    class Database
      public function 
    __construct(Sitewide $sitewide)
    $this->sitewide $sitewide;
      public function 
    // you now have access to sitewide internally:
    echo $this->sitewide->bar();

    $sitewide = new Sitewide();
    $db = new Database($sitewide);
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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