-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add ReflectionProperty::getMangledName()
#18980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
ext/reflection/tests/ReflectionProperty_getMangledName_basic.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--TEST-- | ||
Test ReflectionProperty::getMangledName() method | ||
--FILE-- | ||
<?php | ||
|
||
class TestClass { | ||
public $publicProp = 'public'; | ||
protected $protectedProp = 'protected'; | ||
private $privateProp = 'private'; | ||
} | ||
|
||
function testMangledName($class, $property) { | ||
$reflection = new ReflectionProperty($class, $property); | ||
echo "Property: $property\n"; | ||
echo "getName(): " . $reflection->getName() . "\n"; | ||
echo "getMangledName(): " . $reflection->getMangledName() . "\n"; | ||
|
||
$obj = new $class(); | ||
$array = (array) $obj; | ||
echo "In array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "found" : "not found") . "\n"; | ||
echo "\n"; | ||
} | ||
|
||
testMangledName('TestClass', 'publicProp'); | ||
testMangledName('TestClass', 'protectedProp'); | ||
testMangledName('TestClass', 'privateProp'); | ||
|
||
?> | ||
--EXPECTF-- | ||
Property: publicProp | ||
getName(): publicProp | ||
getMangledName(): publicProp | ||
In array cast: found | ||
|
||
Property: protectedProp | ||
getName(): protectedProp | ||
getMangledName(): %0*%0protectedProp | ||
In array cast: found | ||
|
||
Property: privateProp | ||
getName(): privateProp | ||
getMangledName(): %0TestClass%0privateProp | ||
In array cast: found |
125 changes: 125 additions & 0 deletions
125
ext/reflection/tests/ReflectionProperty_getMangledName_dynamic.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--TEST-- | ||
Test ReflectionProperty::getMangledName() with dynamic properties | ||
--FILE-- | ||
<?php | ||
|
||
echo "=== Testing stdClass with dynamic properties ===\n"; | ||
$stdObj = new stdClass(); | ||
$stdObj->prop1 = 'value1'; | ||
$stdObj->{'special-name'} = 'special value'; | ||
$stdObj->{'123numeric'} = 'numeric start'; | ||
|
||
function testDynamicProperty($obj, $property, $description) { | ||
try { | ||
$reflection = new ReflectionProperty($obj, $property); | ||
echo "$description:\n"; | ||
echo " getName(): " . $reflection->getName() . "\n"; | ||
echo " getMangledName(): " . $reflection->getMangledName() . "\n"; | ||
|
||
$array = (array) $obj; | ||
echo " Found in array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "yes" : "no") . "\n"; | ||
echo "\n"; | ||
} catch (ReflectionException $e) { | ||
echo "$description: EXCEPTION - " . $e->getMessage() . "\n\n"; | ||
} | ||
} | ||
|
||
testDynamicProperty($stdObj, 'prop1', 'stdClass property prop1'); | ||
testDynamicProperty($stdObj, 'special-name', 'stdClass property with special name'); | ||
testDynamicProperty($stdObj, '123numeric', 'stdClass property starting with number'); | ||
|
||
echo "=== Testing edge cases ===\n"; | ||
$numericObj = (object)[true]; | ||
testDynamicProperty($numericObj, '0', 'Property name as number'); | ||
|
||
$nullByteObj = (object)["foo\0" => true]; | ||
testDynamicProperty($nullByteObj, "foo\0", 'Property name with null byte'); | ||
|
||
$invalidObj = (object)["::" => true]; | ||
testDynamicProperty($invalidObj, '::', 'Invalid property name'); | ||
|
||
echo "=== Testing regular class with dynamic properties ===\n"; | ||
#[AllowDynamicProperties] | ||
class TestClass { | ||
public $existing = 'existing'; | ||
} | ||
|
||
$obj = new TestClass(); | ||
$obj->dynamic = 'dynamic value'; | ||
$obj->anotherDynamic = 'another dynamic'; | ||
|
||
testDynamicProperty($obj, 'dynamic', 'Regular class dynamic property'); | ||
testDynamicProperty($obj, 'anotherDynamic', 'Regular class another dynamic property'); | ||
|
||
$reflection = new ReflectionProperty($obj, 'existing'); | ||
echo "Regular property:\n"; | ||
echo " getName(): " . $reflection->getName() . "\n"; | ||
echo " getMangledName(): " . $reflection->getMangledName() . "\n"; | ||
|
||
echo "\n=== Testing ReflectionProperty from class vs instance ===\n"; | ||
try { | ||
$reflection = new ReflectionProperty('TestClass', 'dynamic'); | ||
echo "This should not be reached\n"; | ||
} catch (ReflectionException $e) { | ||
echo "Expected exception for class-based reflection: " . $e->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$reflection = new ReflectionProperty($obj, 'dynamic'); | ||
echo "Instance-based reflection works: " . $reflection->getMangledName() . "\n"; | ||
} catch (ReflectionException $e) { | ||
echo "Unexpected exception: " . $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
alexandre-daubois marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=== Testing stdClass with dynamic properties === | ||
stdClass property prop1: | ||
getName(): prop1 | ||
getMangledName(): prop1 | ||
Found in array cast: yes | ||
|
||
stdClass property with special name: | ||
getName(): special-name | ||
getMangledName(): special-name | ||
Found in array cast: yes | ||
|
||
stdClass property starting with number: | ||
alexandre-daubois marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getName(): 123numeric | ||
getMangledName(): 123numeric | ||
Found in array cast: yes | ||
|
||
=== Testing edge cases === | ||
Property name as number: | ||
getName(): 0 | ||
getMangledName(): 0 | ||
Found in array cast: yes | ||
|
||
Property name with null byte: | ||
getName(): foo%0 | ||
getMangledName(): foo%0 | ||
Found in array cast: yes | ||
|
||
Invalid property name: | ||
getName(): :: | ||
getMangledName(): :: | ||
Found in array cast: yes | ||
|
||
=== Testing regular class with dynamic properties === | ||
Regular class dynamic property: | ||
getName(): dynamic | ||
getMangledName(): dynamic | ||
Found in array cast: yes | ||
|
||
Regular class another dynamic property: | ||
getName(): anotherDynamic | ||
getMangledName(): anotherDynamic | ||
Found in array cast: yes | ||
|
||
Regular property: | ||
getName(): existing | ||
getMangledName(): existing | ||
|
||
=== Testing ReflectionProperty from class vs instance === | ||
Expected exception for class-based reflection: Property TestClass::$dynamic does not exist | ||
Instance-based reflection works: dynamic |
82 changes: 82 additions & 0 deletions
82
ext/reflection/tests/ReflectionProperty_getMangledName_hooks.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--TEST-- | ||
Test ReflectionProperty::getMangledName() with property hooks | ||
--FILE-- | ||
<?php | ||
|
||
echo "=== Testing virtual hooked properties ===\n"; | ||
|
||
class Demo { | ||
protected string $foo { | ||
get => "virtual"; | ||
} | ||
|
||
public string $bar { | ||
get => "hooked getter"; | ||
set => throw new Exception("Cannot set bar"); | ||
} | ||
|
||
public string $baz = "backed"; | ||
} | ||
|
||
$d = new Demo(); | ||
|
||
function testHookedProperty($obj, $property, $description) { | ||
try { | ||
$reflection = new ReflectionProperty($obj, $property); | ||
echo "$description:\n"; | ||
echo " getName(): " . $reflection->getName() . "\n"; | ||
echo " getMangledName(): " . $reflection->getMangledName() . "\n"; | ||
|
||
$array = (array) $obj; | ||
echo " Found in array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "yes" : "no") . "\n"; | ||
echo " Has hooks: " . ($reflection->hasHooks() ? "yes" : "no") . "\n"; | ||
echo "\n"; | ||
} catch (ReflectionException $e) { | ||
echo "$description: EXCEPTION - " . $e->getMessage() . "\n\n"; | ||
} | ||
} | ||
|
||
testHookedProperty($d, 'foo', 'Virtual hooked property (protected)'); | ||
testHookedProperty($d, 'bar', 'Hooked property with getter/setter (public)'); | ||
testHookedProperty($d, 'baz', 'Regular backed property'); | ||
|
||
echo "=== Object dump ===\n"; | ||
var_dump($d); | ||
|
||
echo "\n=== Array cast ===\n"; | ||
var_dump((array)$d); | ||
|
||
?> | ||
--EXPECTF-- | ||
=== Testing virtual hooked properties === | ||
Virtual hooked property (protected): | ||
getName(): foo | ||
getMangledName(): %0*%0foo | ||
Found in array cast: no | ||
Has hooks: yes | ||
|
||
Hooked property with getter/setter (public): | ||
getName(): bar | ||
getMangledName(): bar | ||
Found in array cast: no | ||
Has hooks: yes | ||
|
||
Regular backed property: | ||
getName(): baz | ||
getMangledName(): baz | ||
Found in array cast: yes | ||
Has hooks: no | ||
|
||
=== Object dump === | ||
object(Demo)#1 (1) { | ||
["bar"]=> | ||
uninitialized(string) | ||
["baz"]=> | ||
string(6) "backed" | ||
} | ||
|
||
=== Array cast === | ||
array(1) { | ||
["baz"]=> | ||
string(6) "backed" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.