Error.
Syntax and Initialization
Thereadonly modifier is applied to a property declaration. It can be used alongside visibility modifiers (public, protected, private).
Strict Technical Constraints
1. Explicit Typing Requirement A readonly property must have an explicit type declaration. Untyped properties cannot be marked readonly. If any type is acceptable, themixed type must be explicitly declared.
public.
static. The readonly modifier is strictly tied to instance properties. Attempting to combine them results in a fatal error.
unset().
Shallow Immutability
Thereadonly modifier enforces a strict binding between the property and its assigned value. However, if the assigned value is an object, the internal state of that referenced object remains mutable. The readonly constraint prevents assigning a new object to the property, but does not make the referenced object itself immutable.
Readonly Classes (PHP 8.2+)
Thereadonly modifier can be applied to the class declaration itself. This implicitly marks every declared property within the class as readonly and prevents the creation of dynamic properties.
Reinitialization during Cloning (PHP 8.3+)
As of PHP 8.3, readonly properties can be reinitialized exclusively within the__clone() magic method. This allows for deep cloning of objects containing readonly properties.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More





