[PropertyAccess] Fix accessing dynamic properties #37622
Merged
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.
/cc @joelwurtz @Korbeil WDYT?
On my side, I'm wondering about the perf impact of the implementation. Isn't this the same as removing the
$object instanceof \stdClasscheck?Please also add tests.
Updated PropertyAccessor to access public properties for other classes[PropertyAccess] Fix accessing dynamic propertiesUh oh!
There was an error while loading. Please reload this page.
@nicolas-grekas it will fail when trying to access protected/private property (
$accessis null but we dunno why: it's a dynamic object property or class protected/private inaccessible property). The only reason why \stdClass check was written before - to be 100% sure that class property is dynamic and cannot be private/protected - inaccessible.To improve performance I suggest having
PropertyReadInfoin any case to make a decision looking onPropertyReadInfo::$visibility. But it requires some refactoring.BTW setValue() has the same logic. So I guess it also should support dynamic value?
Update: unit-test added. But only PropertyAccessor::getValue() is covered with getting dynamic value. setValue() - I didn't cover and as I see from unit-tests, the dynamic value cannot be set by design - at least setter method should be.
@dunglas , could you please tell (you created ReflectionExtractor), whether support of dynamic values getting/setting by ref evolutionally (and intentionally) was removed? I just want to be sure not to harm the idea of this tool.
I'm not sure about perf also, it may be slower (but need benchmark for that)
Another possible implementation would be to follow @nicolas-grekas suggestion by only removing the instanceof part and add a try catch when getting the value (to catch error if property is not accessible : protected / private), maybe it would keep perf reasonable and would also allow a better error message (like property exists but is not accessible)
@nicolas-grekas please review the last approach suggested by @joelwurtz .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can add the error in the parent exception
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a previous exception as an argument, hope I've got you right.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
[PropertyAccess] Fix accessing dynamic properties8000 47bd018a59be80to47bd018CompareThank you @andreyserdjuk.
92cb709into symfony:5.1Reverting this PR as it breaks things by bypassing a lot of logic. One such example is when you have a class with a private property AND a __get method to access it (like TestClassMagicGet in our test suite). Before this PR, if you disable magic __get method, you will get an expected NoSuchPropertyException. But now, the property_exists() call will return true, and $object->$property will get the value because there is a __get method (which the code explicitly disallow).
Revert "bug #37622 [PropertyAccess] Fix accessing dynamic properties …47b9b5cMerge branch '5.1'0885ebbIssue #37615 is again an issue since this is reverted. Any update on that? @fabpot @xabbuh @nicolas-grekas