Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

@alexandre-daubois alexandre-daubois commented Jun 30, 2025

Fix #18605

Good news is that we'll be able to leverage this in the next version of Symfony.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! The implementation looks correct. You may want to avoid printing the mangled name directly in the test, given that it will make the file appear as a binary file. Maybe print the hex value instead, or replace the special chars (i.e. \0 to \\0).

@alexandre-daubois alexandre-daubois force-pushed the props-mangling branch 2 times, most recently from 9022201 to 5dd71d0 Compare June 30, 2025 13:46
@alexandre-daubois
Copy link
Contributor Author

replace the special chars (i.e. \0 to \0)

Nice idea! PR updated, thanks!

@TimWolla
Copy link
Member

TimWolla commented Jul 1, 2025

FWIW: The test format also supports the %0 placeholder for this purpose:

'%0' => '\x00',

@alexandre-daubois
Copy link
Contributor Author

I tried to swap for %0 but the tests fail. I can try to spend more time on it if you think it should really be used 🙂 Both ways suit me

@iluuu1994
Copy link
Member

Maybe you forgot to switch to EXPECTF?

@alexandre-daubois
Copy link
Contributor Author

Exactly this 🤦 Thank you!

@iluuu1994
Copy link
Member

Thanks! LGTM. Waiting on @DanielEScherzer's review as reflection maintainer.

Copy link
Member

@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but there are a few edge cases that should probably be tested

  • Dynamic class properties
  • Class properties that override parent properties (both for public and protected, and both for maintaining the same visibility and increasing it (for protected->public))
  • using ReflectionProperty from a specific instance, rather than from a class - the instance might have a dynamic property that the class doesn't
  • and more specifically, ReflectionProperty from an instance of stdClass

@alexandre-daubois
Copy link
Contributor Author

Good catch, ref->prop is null when dealing with dynamic properties. Given they are always public, we can directly return their unmangled name.

echo "=== Testing stdClass with dynamic properties ===\n";
$stdObj = new stdClass();
$stdObj->prop1 = 'value1';
$stdObj->prop2 = 'value2';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prop2 never gets used or read, is that intentional?

}

?>
--EXPECTF--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any placeholders here

Suggested change
--EXPECTF--
--EXPECT--

$obj = new $class();
$array = (array) $obj;

echo "Class: $class, Property: $property\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Class: $class, Property: $property\n";
echo "Class: $class, Property: \$$property\n";

because some of the property names are also visibility indicators, and the output is confusing in that case - is it a property named "public", or one that is public?

testProperty('ChildClass', 'public');
testProperty('ChildClass', 'protected');
testProperty('ChildClass', 'childProp');
testProperty('ChildClass', 'private');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also test with a private property on the parent class that does not get shadowed with a property of the same name in the child class? In both instance and class based creation of ReflectionProperty the constructor should fail, but its good to confirm so that if it ever changes we have a test that gets updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce ReflectionProperty::getMangledName()
4 participants