1--TEST-- 2Modifying a readonly property 3--FILE-- 4<?php 5 6class Test { 7 readonly public int $prop; 8 readonly public array $prop2; 9 10 public function __construct() { 11 // Initializing assignments. 12 $this->prop = 1; 13 $this->prop2 = []; 14 } 15} 16 17function byRef(&$ref) {} 18 19$test = new Test; 20var_dump($test->prop); // Read. 21try { 22 $test->prop = 2; 23} catch (Error $e) { 24 echo $e->getMessage(), "\n"; 25} 26try { 27 $test->prop += 1; 28} catch (Error $e) { 29 echo $e->getMessage(), "\n"; 30} 31try { 32 $test->prop++; 33} catch (Error $e) { 34 echo $e->getMessage(), "\n"; 35} 36try { 37 ++$test->prop; 38} catch (Error $e) { 39 echo $e->getMessage(), "\n"; 40} 41try { 42 $ref =& $test->prop; 43} catch (Error $e) { 44 echo $e->getMessage(), "\n"; 45} 46try { 47 $test->prop =& $ref; 48} catch (Error $e) { 49 echo $e->getMessage(), "\n"; 50} 51try { 52 byRef($test->prop); 53} catch (Error $e) { 54 echo $e->getMessage(), "\n"; 55} 56 57var_dump($test->prop2); // Read. 58try { 59 $test->prop2[] = 1; 60} catch (Error $e) { 61 echo $e->getMessage(), "\n"; 62} 63try { 64 $test->prop2[0][] = 1; 65} catch (Error $e) { 66 echo $e->getMessage(), "\n"; 67} 68 69?> 70--EXPECT-- 71int(1) 72Cannot modify readonly property Test::$prop 73Cannot modify readonly property Test::$prop 74Cannot modify readonly property Test::$prop 75Cannot modify readonly property Test::$prop 76Cannot indirectly modify readonly property Test::$prop 77Cannot indirectly modify readonly property Test::$prop 78Cannot indirectly modify readonly property Test::$prop 79array(0) { 80} 81Cannot indirectly modify readonly property Test::$prop2 82Cannot indirectly modify readonly property Test::$prop2 83