Can Clone Readonly Properties

Readonly properties may be changed, both at constructor and cloning time, since PHP 8.3. Until then, once set, they could never be changed.

PHP code

<?php

class X {
     readonly int $property;
     readonly A $property2;

     function __construct(int $p) {
             $this->property = $p;
             $this->property2 = new A($p);
     }

     function __clone() {
             $this->property++; // clone used to change scalar
             $this->property2 = new A($this->property); // clone used to change object
     }
}

$x = new X;
$y = clone $x;

var_dump($y);

?>

Before

PHP Fatal error:  Uncaught Error: Cannot modify readonly property x::$p

Fatal error: Uncaught Error: Cannot modify readonly property x::$p

After

object(x)#2 (1) {
  ["p"]=>
  int(3)
}

PHP version change

This behavior changed in 8.3

Error Messages