1--TEST-- 2Test that readonly properties cannot be reassigned by invoking the __clone() method directly 3--FILE-- 4<?php 5 6class Foo 7{ 8 public function __construct( 9 public readonly int $bar 10 ) {} 11 12 public function __clone() 13 { 14 $this->bar = 1; 15 } 16} 17 18$foo = new Foo(0); 19 20var_dump($foo); 21 22try { 23 $foo->__clone(); 24} catch (Error $e) { 25 echo $e->getMessage() . "\n"; 26} 27 28try { 29 $foo->__clone(); 30} catch (Error $e) { 31 echo $e->getMessage() . "\n"; 32} 33 34var_dump($foo); 35 36?> 37--EXPECTF-- 38object(Foo)#%d (%d) { 39 ["bar"]=> 40 int(0) 41} 42Cannot modify readonly property Foo::$bar 43Cannot modify readonly property Foo::$bar 44object(Foo)#%d (%d) { 45 ["bar"]=> 46 int(0) 47} 48