|
| 1 | +--TEST-- |
| 2 | +Test ReflectionProperty::getMangledName() with dynamic properties |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +echo "=== Testing stdClass with dynamic properties ===\n"; |
| 7 | +$stdObj = new stdClass(); |
| 8 | +$stdObj->prop1 = 'value1'; |
| 9 | +$stdObj->{'special-name'} = 'special value'; |
| 10 | +$stdObj->{'123numeric'} = 'numeric start'; |
| 11 | + |
| 12 | +function testDynamicProperty($obj, $property, $description) { |
| 13 | + try { |
| 14 | + $reflection = new ReflectionProperty($obj, $property); |
| 15 | + echo "$description:\n"; |
| 16 | + echo " getName(): " . $reflection->getName() . "\n"; |
| 17 | + echo " getMangledName(): " . $reflection->getMangledName() . "\n"; |
| 18 | + |
| 19 | + $array = (array) $obj; |
| 20 | + echo " Found in array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "yes" : "no") . "\n"; |
| 21 | + echo "\n"; |
| 22 | + } catch (ReflectionException $e) { |
| 23 | + echo "$description: EXCEPTION - " . $e->getMessage() . "\n\n"; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +testDynamicProperty($stdObj, 'prop1', 'stdClass property prop1'); |
| 28 | +testDynamicProperty($stdObj, 'special-name', 'stdClass property with special name'); |
| 29 | +testDynamicProperty($stdObj, '123numeric', 'stdClass property starting with number'); |
| 30 | + |
| 31 | +echo "=== Testing edge cases ===\n"; |
| 32 | +$numericObj = (object)[true]; |
| 33 | +testDynamicProperty($numericObj, '0', 'Property name as number'); |
| 34 | + |
| 35 | +$nullByteObj = (object)["foo\0" => true]; |
| 36 | +testDynamicProperty($nullByteObj, "foo\0", 'Property name with null byte'); |
| 37 | + |
| 38 | +$invalidObj = (object)["::" => true]; |
| 39 | +testDynamicProperty($invalidObj, '::', 'Invalid property name'); |
| 40 | + |
| 41 | +echo "=== Testing regular class with dynamic properties ===\n"; |
| 42 | +#[AllowDynamicProperties] |
| 43 | +class TestClass { |
| 44 | + public $existing = 'existing'; |
| 45 | +} |
| 46 | + |
| 47 | +$obj = new TestClass(); |
| 48 | +$obj->dynamic = 'dynamic value'; |
| 49 | +$obj->anotherDynamic = 'another dynamic'; |
| 50 | + |
| 51 | +testDynamicProperty($obj, 'dynamic', 'Regular class dynamic property'); |
| 52 | +testDynamicProperty($obj, 'anotherDynamic', 'Regular class another dynamic property'); |
| 53 | + |
| 54 | +$reflection = new ReflectionProperty($obj, 'existing'); |
| 55 | +echo "Regular property:\n"; |
| 56 | +echo " getName(): " . $reflection->getName() . "\n"; |
| 57 | +echo " getMangledName(): " . $reflection->getMangledName() . "\n"; |
| 58 | + |
| 59 | +echo "\n=== Testing ReflectionProperty from class vs instance ===\n"; |
| 60 | +try { |
| 61 | + $reflection = new ReflectionProperty('TestClass', 'dynamic'); |
| 62 | + echo "This should not be reached\n"; |
| 63 | +} catch (ReflectionException $e) { |
| 64 | + echo "Expected exception for class-based reflection: " . $e->getMessage() . "\n"; |
| 65 | +} |
| 66 | + |
| 67 | +try { |
| 68 | + $reflection = new ReflectionProperty($obj, 'dynamic'); |
| 69 | + echo "Instance-based reflection works: " . $reflection->getMangledName() . "\n"; |
| 70 | +} catch (ReflectionException $e) { |
| 71 | + echo "Unexpected exception: " . $e->getMessage() . "\n"; |
| 72 | +} |
| 73 | + |
| 74 | +?> |
| 75 | +--EXPECTF-- |
| 76 | +=== Testing stdClass with dynamic properties === |
| 77 | +stdClass property prop1: |
| 78 | + getName(): prop1 |
| 79 | + getMangledName(): prop1 |
| 80 | + Found in array cast: yes |
| 81 | + |
| 82 | +stdClass property with special name: |
| 83 | + getName(): special-name |
| 84 | + getMangledName(): special-name |
| 85 | + Found in array cast: yes |
| 86 | + |
| 87 | +stdClass property starting with number: |
| 88 | + getName(): 123numeric |
| 89 | + getMangledName(): 123numeric |
| 90 | + Found in array cast: yes |
| 91 | + |
| 92 | +=== Testing edge cases === |
| 93 | +Property name as number: |
| 94 | + getName(): 0 |
| 95 | + getMangledName(): 0 |
| 96 | + Found in array cast: yes |
| 97 | + |
| 98 | +Property name with null byte: |
| 99 | + getName(): foo%0 |
| 100 | + getMangledName(): foo%0 |
| 101 | + Found in array cast: yes |
| 102 | + |
| 103 | +Invalid property name: |
| 104 | + getName(): :: |
| 105 | + getMangledName(): :: |
| 106 | + Found in array cast: yes |
| 107 | + |
| 108 | +=== Testing regular class with dynamic properties === |
| 109 | +Regular class dynamic property: |
| 110 | + getName(): dynamic |
| 111 | + getMangledName(): dynamic |
| 112 | + Found in array cast: yes |
| 113 | + |
| 114 | +Regular class another dynamic property: |
| 115 | + getName(): anotherDynamic |
| 116 | + getMangledName(): anotherDynamic |
| 117 | + Found in array cast: yes |
| 118 | + |
| 119 | +Regular property: |
| 120 | + getName(): existing |
| 121 | + getMangledName(): existing |
| 122 | + |
| 123 | +=== Testing ReflectionProperty from class vs instance === |
| 124 | +Expected exception for class-based reflection: Property TestClass::$dynamic does not exist |
| 125 | +Instance-based reflection works: dynamic |
0 commit comments