-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Fix accessing dynamic properties #37622
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
Conversation
edited by nicolas-grekas
Q | A |
---|---|
Branch? | 5.1 (to be switched when merging) |
Bug fix? | yes |
New feature? | no |
Deprecations? | no |
Tickets | Fix #37026 |
License | MIT |
Doc PR | no |
/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 Please also add tests. |
@nicolas-grekas it will fail when trying to access protected/private property ( |
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. |
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 . |
} | ||
} catch (\Error $e) { | ||
if (!$ignoreInvalidProperty) { | ||
throw new NoSuchPropertyException(sprintf('Can\'t read protected or private property "%s" in class "%s".', $property, $class)); |
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
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.
Thank you @andreyserdjuk. |
Reverting 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). |
* 5.1: Revert "bug #37622 [PropertyAccess] Fix accessing dynamic properties (andreyserdjuk)"
Issue #37615 is again an issue since this is reverted. Any update on that? @fabpot @xabbuh @nicolas-grekas |