Skip to content

Conversation

@neznaika0
Copy link
Contributor

Description
I'm doing another PR and in the tests I found that anonymous classes cannot access parent properties. I hope this fixes it.

I want to note that there will be problems when both classes have the same properties - the reflection of the helper changes only the child. Any suggestions?

Example:

// Foo class
$obj->private = 'root';

// anon class with Foo
$child->private = 'child';
// helper will only change the child, the parent will contain the old value 
// for example, call from the root method
$child->getPrivate() // root

The guide, as always, corrects the translation.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide


$refClass = new ReflectionObject($obj);

if (! $refClass->hasProperty($property) && str_contains($obj::class, '@anonymous')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you know how duplicate properties should work correctly, you can fix it here. When properties exist in both classes

@neznaika0 neznaika0 mentioned this pull request Jan 8, 2026
5 tasks
@michalsn
Copy link
Member

michalsn commented Jan 9, 2026

I don't think this is a good idea.

Reflection exposes class semantics as they are. Private properties belong strictly to the declaring class. Automatically scanning parent private properties would make private appear inherited, which it is not. The helper would introduce a misleading abstraction, so the current behavior should remain unchanged.

@neznaika0
Copy link
Contributor Author

@michalsn In my opinion, if we need access to private properties, are we allowed to change the parent values?
Is that the idea of an helper, not?

@michalsn
Copy link
Member

michalsn commented Jan 9, 2026

A helper should make common tasks easier, not redefine visibility rules. Parent private properties are not part of the child's API, and Reflection does not change that.

@neznaika0
Copy link
Contributor Author

Sadly. Overdoing it again. Let's wait @paulbalandan @samsonasik

Do you think it's okay to duplicate private properties in an anonymous class? The parent will not change, only the child.

@paulbalandan
Copy link
Member

In this example:

// Foo class
$obj->private = 'root';

// anon class with Foo
$child->private = 'child';
// helper will only change the child, the parent will contain the old value 
// for example, call from the root method
$child->getPrivate() // root

in my opinion, $child->getPrivate() should return the private property of the child class as it has no knowledge of the parent's private properties. The same should go with reflection.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants