www.webdeveloper.com
Results 1 to 4 of 4

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

  1. #1
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    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
    Location
    The City of Roses
    Posts
    2,503
    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.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Apr 2010
    Posts
    227
    Aye
    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
    Location
    Ankh-Morpork
    Posts
    19,151
    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
    {
      private 
    $sitewide;
      public function 
    __construct(Sitewide $sitewide)
      {
        
    $this->sitewide $sitewide;
      }
      public function 
    foo()
      {
        
    // you now have access to sitewide internally:
        
    echo $this->sitewide->bar();
      }
    }

    $sitewide = new Sitewide();
    $db = new Database($sitewide);
    $db->foo(); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles