|
| 1 | +--TEST-- |
| 2 | +GH-24147: Reflection $name and $class properties cannot be modified |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class B { |
| 7 | + public string $x; |
| 8 | +} |
| 9 | + |
| 10 | +function byRef(&$x) { $x = 'Z'; } |
| 11 | + |
| 12 | +$tests = [ |
| 13 | + 'compound assignment' => function ($r) { $r->class .= 'Z'; }, |
| 14 | + 'reference' => function ($r) { $ref = &$r->name; $ref = 'Z'; }, |
| 15 | + 'string offset' => function ($r) { $r->name[0] = 'Z'; }, |
| 16 | + 'by-ref argument' => function ($r) { byRef($r->class); }, |
| 17 | + 'dynamic by-ref argument' => function ($r) { $fn = 'byRef'; $fn($r->class); }, |
| 18 | + 'foreach by reference' => function ($r) { foreach ($r as &$v) { $v = 'Z'; } }, |
| 19 | + 'unset' => function ($r) { unset($r->name); }, |
| 20 | + 'setValue' => function ($r) { (new ReflectionProperty($r, 'name'))->setValue($r, 'Z'); }, |
| 21 | + 'setRawValue' => function ($r) { (new ReflectionProperty($r, 'name'))->setRawValue($r, 'Z'); }, |
| 22 | +]; |
| 23 | + |
| 24 | +foreach ($tests as $label => $test) { |
| 25 | + $r = new ReflectionProperty('B', 'x'); |
| 26 | + try { |
| 27 | + $test($r); |
| 28 | + echo "$label: no error\n"; |
| 29 | + } catch (Throwable $e) { |
| 30 | + echo "$label: ", $e::class, ": ", $e->getMessage(), "\n"; |
| 31 | + } |
| 32 | + if (!isset($r->name, $r->class) || $r->name !== $r->getName() || $r->class !== $r->getDeclaringClass()->name) { |
| 33 | + echo "$label: properties diverged from getters\n"; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class WriteBeforeConstruct extends ReflectionClass { |
| 38 | + public function __construct() { |
| 39 | + $this->name = 'Z'; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class UnsetAfterConstruct extends ReflectionClass { |
| 44 | + public function __construct() { |
| 45 | + parent::__construct('B'); |
| 46 | + unset($this->name); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +foreach (['WriteBeforeConstruct', 'UnsetAfterConstruct'] as $class) { |
| 51 | + try { |
| 52 | + new $class(); |
| 53 | + echo "$class: no error\n"; |
| 54 | + } catch (Throwable $e) { |
| 55 | + echo "$class: ", $e::class, ": ", $e->getMessage(), "\n"; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Unsetting the uninitialized property in a subclass to route reads through __get() stays possible |
| 60 | +class UnsetBeforeConstruct extends ReflectionClass { |
| 61 | + public function __construct() { |
| 62 | + unset($this->name); |
| 63 | + } |
| 64 | + |
| 65 | + public function __get($name) { |
| 66 | + return "magic $name"; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +var_dump((new UnsetBeforeConstruct())->name); |
| 71 | + |
| 72 | +foreach (['setValue', 'setRawValue'] as $method) { |
| 73 | + $r = (new ReflectionClass('ReflectionClass'))->newInstanceWithoutConstructor(); |
| 74 | + try { |
| 75 | + (new ReflectionProperty($r, 'name'))->$method($r, 'Z'); |
| 76 | + echo "uninitialized $method: no error\n"; |
| 77 | + } catch (Throwable $e) { |
| 78 | + echo "uninitialized $method: ", $e::class, ": ", $e->getMessage(), "\n"; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +?> |
| 83 | +--EXPECT-- |
| 84 | +compound assignment: ReflectionException: Cannot set read-only property ReflectionProperty::$class |
| 85 | +reference: Error: Cannot indirectly modify readonly property ReflectionProperty::$name |
| 86 | +string offset: Error: Cannot indirectly modify readonly property ReflectionProperty::$name |
| 87 | +by-ref argument: Error: Cannot indirectly modify readonly property ReflectionProperty::$class |
| 88 | +dynamic by-ref argument: Error: Cannot indirectly modify readonly property ReflectionProperty::$class |
| 89 | +foreach by reference: Error: Cannot acquire reference to readonly property ReflectionProperty::$name |
| 90 | +unset: Error: Cannot unset readonly property ReflectionProperty::$name |
| 91 | +setValue: ReflectionException: Cannot set read-only property ReflectionProperty::$name |
| 92 | +setRawValue: ReflectionException: Cannot set read-only property ReflectionProperty::$name |
| 93 | +WriteBeforeConstruct: ReflectionException: Cannot set read-only property WriteBeforeConstruct::$name |
| 94 | +UnsetAfterConstruct: Error: Cannot unset readonly property ReflectionClass::$name |
| 95 | +string(10) "magic name" |
| 96 | +uninitialized setValue: ReflectionException: Cannot set read-only property ReflectionClass::$name |
| 97 | +uninitialized setRawValue: ReflectionException: Cannot set read-only property ReflectionClass::$name |
0 commit comments