1--TEST-- 2Not-modifying a readonly property holding an object 3--FILE-- 4<?php 5 6class Test { 7 public readonly object $prop; 8 9 public function __construct(object $prop) { 10 $this->prop = $prop; 11 } 12} 13 14$test = new Test(new stdClass); 15$test->prop->foo = 1; 16$test->prop->foo += 1; 17$test->prop->foo++; 18try { 19 $test->prop += 1; 20} catch (Error $e) { 21 echo $e->getMessage(), "\n"; 22} 23try { 24 $test->prop++; 25} catch (Error $e) { 26 echo $e->getMessage(), "\n"; 27} 28try { 29 --$test->prop; 30} catch (Error $e) { 31 echo $e->getMessage(), "\n"; 32} 33var_dump($test->prop); 34 35// Unfortunately this is allowed, but does not modify $test->prop. 36$ref =& $test->prop; 37$ref = new stdClass; 38var_dump($test->prop); 39 40$test = new Test(new ArrayObject()); 41$test->prop[] = []; 42$test->prop[0][] = 1; 43var_dump($test->prop); 44 45?> 46--EXPECT-- 47Unsupported operand types: stdClass + int 48Cannot modify readonly property Test::$prop 49Cannot modify readonly property Test::$prop 50object(stdClass)#2 (1) { 51 ["foo"]=> 52 int(3) 53} 54object(stdClass)#2 (1) { 55 ["foo"]=> 56 int(3) 57} 58object(ArrayObject)#7 (1) { 59 ["storage":"ArrayObject":private]=> 60 array(1) { 61 [0]=> 62 array(1) { 63 [0]=> 64 int(1) 65 } 66 } 67} 68