Skip to content

Commit ef72bbf

Browse files
Add ReflectionProperty::getMangledName()
1 parent c9249e2 commit ef72bbf

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

‎NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ PHP NEWS
187187
zval for uninitialized typed properties). (nielsdos)
188188
. Fixed bug GH-15766 (ReflectionClass::toString() should have better output
189189
for enums). (DanielEScherzer)
190+
. Added ReflectionProperty::getMangledName() method. (alexandre-daubois)
190191

191192
- Session:
192193
. session_start() throws a ValueError on option argument if not a hashmap

‎ext/reflection/php_reflection.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5753,6 +5753,17 @@ ZEND_METHOD(ReflectionProperty, getName)
57535753
}
57545754
/* }}} */
57555755

5756+
ZEND_METHOD(ReflectionProperty, getMangledName)
5757+
{
5758+
reflection_object *intern;
5759+
property_reference *ref;
5760+
5761+
ZEND_PARSE_PARAMETERS_NONE();
5762+
5763+
GET_REFLECTION_OBJECT_PTR(ref);
5764+
RETURN_STR_COPY(ref->prop->name);
5765+
}
5766+
57565767
static void _property_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
57575768
{
57585769
reflection_object *intern;

‎ext/reflection/php_reflection.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ public function __toString(): string {}
482482
/** @tentative-return-type */
483483
public function getName(): string {}
484484

485+
public function getMangledName(): string {}
486+
485487
/** @tentative-return-type */
486488
public function getValue(?object $object = null): mixed {}
487489

‎ext/reflection/php_reflection_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Test ReflectionProperty::getMangledName() method
3+
--FILE--
4+
<?php
5+
6+
class TestClass {
7+
public $publicProp = 'public';
8+
protected $protectedProp = 'protected';
9+
private $privateProp = 'private';
10+
}
11+
12+
function testMangledName($class, $property) {
13+
$reflection = new ReflectionProperty($class, $property);
14+
echo "Property: $property\n";
15+
echo "getName(): " . $reflection->getName() . "\n";
16+
echo "getMangledName(): " . $reflection->getMangledName() . "\n";
17+
18+
$obj = new $class();
19+
$array = (array) $obj;
20+
echo "In array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "found" : "not found") . "\n";
21+
echo "\n";
22+
}
23+
24+
testMangledName('TestClass', 'publicProp');
25+
testMangledName('TestClass', 'protectedProp');
26+
testMangledName('TestClass', 'privateProp');
27+
28+
?>
29+
--EXPECT--
30+
Property: publicProp
31+
getName(): publicProp
32+
getMangledName(): publicProp
33+
In array cast: found
34+
35+
Property: protectedProp
36+
getName(): protectedProp
37+
getMangledName(): *protectedProp
38+
In array cast: found
39+
40+
Property: privateProp
41+
getName(): privateProp
42+
getMangledName(): TestClassprivateProp
43+
In array cast: found
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
Test ReflectionProperty::getMangledName() with inheritance
3+
--FILE--
4+
<?php
5+
6+
class ParentClass {
7+
public $public = 'parent_public';
8+
protected $protected = 'parent_protected';
9+
private $private = 'parent_private';
10+
}
11+
12+
class ChildClass extends ParentClass {
13+
private $private = 'child_private';
14+
protected $childProp = 'child_protected';
15+
}
16+
17+
function testProperty($class, $property) {
18+
$reflection = new ReflectionProperty($class, $property);
19+
$obj = new $class();
20+
$array = (array) $obj;
21+
22+
echo "Class: $class, Property: $property\n";
23+
echo "Mangled name: '" . $reflection->getMangledName() . "'\n";
24+
echo "Key exists in array cast: " . (array_key_exists($reflection->getMangledName(), $array) ? "yes" : "no") . "\n";
25+
echo "\n";
26+
}
27+
28+
testProperty('ParentClass', 'public');
29+
testProperty('ParentClass', 'protected');
30+
testProperty('ParentClass', 'private');
31+
32+
testProperty('ChildClass', 'public');
33+
testProperty('ChildClass', 'protected');
34+
testProperty('ChildClass', 'childProp');
35+
testProperty('ChildClass', 'private');
36+
37+
?>
38+
--EXPECT--
39+
Class: ParentClass, Property: public
40+
Mangled name: 'public'
41+
Key exists in array cast: yes
42+
43+
Class: ParentClass, Property: protected
44+
Mangled name: '*protected'
45+
Key exists in array cast: yes
46+
47+
Class: ParentClass, Property: private
48+
Mangled name: 'ParentClassprivate'
49+
Key exists in array cast: yes
50+
51+
Class: ChildClass, Property: public
52+
Mangled name: 'public'
53+
Key exists in array cast: yes
54+
55+
Class: ChildClass, Property: protected
56+
Mangled name: '*protected'
57+
Key exists in array cast: yes
58+
59+
Class: ChildClass, Property: childProp
60+
Mangled name: '*childProp'
61+
Key exists in array cast: yes
62+
63+
Class: ChildClass, Property: private
64+
Mangled name: 'ChildClassprivate'
65+
Key exists in array cast: yes

0 commit comments

Comments
 (0)