SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inheritance Question

    I have a child class inheriting most of its methods from a parent class.

    But when I instantiate the child class, the $this operator in the parent class code isn't accessing any of the object variables.

    I'm not sure what I'm missing here. The parent class should be able to access object properties with $this, shouldn't it???

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    The parent class can access only parent class properties. The child class can access both parent and child class properties, unless the parent class property is private.
    "First make it work. Then make it better."

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Nope, the child class can access properties from the parent class, but not the other way around.

    Consider a scenario where you have a parent class with multiple children, and all of them are instantiated. Which of the children should $this point to? All of them? And what if multiple children add the same variable? It's just not feasible, which is why it's a one-way street so to speak
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification. Don't know how I've made it this long thinking all the parent class members are encapsulated into the object of a child class. I'll have to redesign the program, likely based on composition rather than inheritance.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by brandonBuster View Post
    Don't know how I've made it this long thinking all the parent class members are encapsulated into the object of a child class.
    They are, but not the other way around. All child classes can see themselves and the parent class, while the parent class can only see itself and any parent classes it might have.

    So if you have

    A > B > C

    (Where > denotes ’is parent of’)

    Then C can see everything in itself, B and A. B can see everything in itself and everything in A. A can only see everything in itself.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •