1--TEST-- 2Unset readonly property 3--FILE-- 4<?php 5 6class Test { 7 public readonly int $prop; 8 9 public function __construct(int $prop) { 10 $this->prop = $prop; 11 } 12} 13 14$test = new Test(1); 15try { 16 unset($test->prop); 17} catch (Error $e) { 18 echo $e->getMessage(), "\n"; 19} 20 21class Test2 { 22 public readonly int $prop; 23 24 public function __construct() { 25 unset($this->prop); // Unset uninitialized. 26 unset($this->prop); // Unset unset. 27 } 28 29 public function __get($name) { 30 // Lazy init. 31 echo __METHOD__, "\n"; 32 $this->prop = 1; 33 return $this->prop; 34 } 35} 36 37$test = new Test2; 38var_dump($test->prop); // Call __get. 39var_dump($test->prop); // Don't call __get. 40try { 41 unset($test->prop); // Unset initialized, illegal. 42} catch (Error $e) { 43 echo $e->getMessage(), "\n"; 44} 45 46class Test3 { 47 public readonly int $prop; 48} 49 50$test = new Test3; 51try { 52 unset($test->prop); 53} catch (Error $e) { 54 echo $e->getMessage(), "\n"; 55} 56 57?> 58--EXPECT-- 59Cannot unset readonly property Test::$prop 60Test2::__get 61int(1) 62int(1) 63Cannot unset readonly property Test2::$prop 64Cannot unset readonly property Test3::$prop from global scope 65