string(5) "World"
Hello World Class inheritance appears to work both ways - both up and down the chain!

Class inheritance appears to work both ways - both up and down the chain!

I hadn't realised that class inheritance works both ways! Both down and up!

Discovered by accident... A method in the base/parent class can reference a property declared as protected in the extended child class! I previously thought it was just the other way around.

May be this seems obvious, but it wasn't to me. In fact in my mind it kind of goes against the grain of good OOP...

This isn't necessarily good OOP, since the base class is dependent on the extended class and essentially becomes abstract. Although this can be accounted for, so that the base works independently, whether it is extended or not. You could simply check that the property exists in the base class first.

If this is the case, then 'Hello World' should be printed twice.

1. From an instance of the extended class... Normal inheritance, child (extended) class method called:

Hello World

2. From an instance of the extended class... Reverse inheritance, parent (base) class method called:

string(5) "World"
Hello World

3. From an instance of the base class... The parent (base) class method called:

NULL
Hello

[Home]