Initialize Readonly On Child

<?php

class x {
    public readonly int $property;
}

class y extends x {
    function __construct() {
        $this->property = 5;
    }
}

$x = new x;
echo $x->property;

Readonly properties could only be initialized in the same class as their definition. That holds true, whatever the visibility of the property: private, protected, private.

In PHP 8.4, it is now possible to initialize the readonly properties from a child class, if the visibility allows. And the property is assigned only once.

See Also

PHP Error Messages

PHP Features

Last updated: 14 July 2026