[PropertyAccess] Fix accessing dynamic properties#37622
Conversation
|
/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.
you can add the error in the parent exception
There was a problem hiding this comment.
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 |
Uh oh!
There was an error while loading. Please reload this page.