PHP Inheritance of a class property’s visibility
In Object-Oriented PHP5 a class’s properties can have their ‘visibility‘ set.
If a property is undeclared in a sub class it inherits the public and protected class properties from its parent classes.
So, how does a property’s visibility square with inheritance (in PHP Version 5.2.9)?
As we travel down the class hierarchy a property CANNOT become LESS visible.
- a public property in the parent CANNOT be declared as protected in the subclass (it will throw a fatal error)
- a protected property in the parent CANNOT be declared as private in the subclass (it will throw a fatal error)
As we travel down the class hierarchy a property CAN become MORE visible.
- a private property in the parent CAN be declared as protected in the subclass (But be aware of scope issues with private properties)
- a protected property in the parent CAN be declared as public in the subclass
Also see: no PHP error thrown when private property accessed or modified by subclass
