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???

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.

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 :slight_smile:

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.

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.