Cannot indirectly modify readonly

<?php

class X {
    readonly public string $property;

    public function __construct() {
        $this->property = 1;
    }
}

$object = new x();
$reference =& $object->property;
//Cannot modify readonly property X::$property

class Y {
    readonly public string $property;

    public function __construct(&$a) {
        // first time assignation
        $this->property = &$a;
        //Cannot modify readonly property X::$property
    }
}

$a = 'A';
$object = new Y($a);

The same error message ‘Cannot indirectly modify readonly property X::$property’ is used when trying to sneak a reference on a readonly property, and updated it later.

Strangely, this is also the same error message when the readonly property is initially assigned a reference, for later sneaky update.

The error is the same, indeed, yet may be these messages may be distinct.

See Also

PHP Error Messages

PHP Features

Last updated: 14 July 2026