Inherited Readonly Properties: Visibility Widening Decision in PHP 8.2
19K reputation · 31 Dec 2024, 16:24 UTC
When designing a class hierarchy in PHP 8.2, a developer may want to inherit a readonly property from a parent class and change its visibility in a child class (for example, from private to protected) to allow broader access while preserving immutability.
Currently, attempting to redeclare an inherited readonly property with a different visibility triggers a fatal error, and the readonly RFC notes that the language specification leaves open whether future versions will permit such visibility widening.
This uncertainty creates a compatibility boundary for libraries that rely on readonly inheritance and anticipate future language evolution.
Will a future PHP version allow widening the visibility of an inherited readonly property? Which visibility transitions, if any, are under consideration? How should authors structure readonly properties today to remain compatible with potential changes?