Cannot %s readonly property %s::$%s from %s%s¶
Description¶
Until PHP 8.4, the assignation (first and final), of a readonly property had to be executed in the definition class. In the illustration, this means that $property
must be assigned in the X
class. This was applied all the time, without consideration for the visibility of the property.
Since PHP 8.4, visibility allowing, a readonly property may be defined in any of the child classes too.
Example¶
<?php
class X {
public readonly int $property;
}
class Y extends X {
function __construct() {
$this->property = 5;
}
}
$x = new Y;
echo $x->property;
?>
Literal Examples¶
Cannot initialize readonly property X::$property from scope Y
Cannot initialize readonly property X::$property from global scope
Solutions¶
Upgrade to PHP 8.4.
Remove the readonly option.