1--TEST-- 2Initialization can only happen from private scope 3--FILE-- 4<?php 5 6class A { 7 public readonly int $prop; 8 9 public function initPrivate() { 10 $this->prop = 3; 11 } 12} 13class B extends A { 14 public function initProtected() { 15 $this->prop = 2; 16 } 17} 18 19$test = new B; 20try { 21 $test->prop = 1; 22} catch (Error $e) { 23 echo $e->getMessage(), "\n"; 24} 25try { 26 $test->initProtected(); 27} catch (Error $e) { 28 echo $e->getMessage(), "\n"; 29} 30 31$test->initPrivate(); 32var_dump($test->prop); 33 34// Rebinding bypass works. 35$test = new B; 36(function() { 37 $this->prop = 1; 38})->bindTo($test, A::class)(); 39var_dump($test->prop); 40 41class C extends A { 42 public readonly int $prop; 43} 44 45$test = new C; 46$test->initPrivate(); 47var_dump($test->prop); 48 49class X { 50 public function initFromParent() { 51 $this->prop = 1; 52 } 53} 54class Y extends X { 55 public readonly int $prop; 56} 57 58$test = new Y; 59try { 60 $test->initFromParent(); 61} catch (Error $e) { 62 echo $e->getMessage(), "\n"; 63} 64 65?> 66--EXPECT-- 67Cannot initialize readonly property A::$prop from global scope 68Cannot initialize readonly property A::$prop from scope B 69int(3) 70int(1) 71int(3) 72Cannot initialize readonly property Y::$prop from scope X 73