Bug #63463 [Com]: ReflectionProperty::setValue and ::getValue trigger magic methods
| From: | benjamin dot morel at gmail dot com | Date: | Thu, 13 Dec 2018 12:13:27 +0000 |
| Subject: | Bug #63463 [Com]: ReflectionProperty::setValue and ::getValue trigger magic methods | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-218430@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63463&edit=1
ID: 63463
Comment by: benjamin dot morel at gmail dot com
Reported by: ocramius at gmail dot com
Summary: ReflectionProperty::setValue and ::getValue trigger
magic methods
Status: Not a bug
Type: Bug
Package: Reflection related
Operating System: Irrelevant
PHP Version: 5.4.8
Block user comment: N
Private report: N
New Comment:
I was just bitten by the same "feature"; I guess my use case is similar to Ocramius':
proxies.
I'm unset()ing properties in the proxy constructor, and using ReflectionProperty::getValue() to
read the properties, set or not.
Unfortunately, doing so triggers __get(), which initializes my proxy.
Both points of views expressed above are valid IMO:
- viewing Reflection as just another way to access the property (Stas)
- viewing Reflection as a low-level tool to access properties without triggering the usual behaviour
(Ocramius)
The problem with the current implementation is that it doesn't help create lazy initialization
scenarios, a feature that's good for the PHP ecosystem.
Evan's suggestion is excellent IMO, as it fixes this issue without breaking BC:
ReflectionProperty::setValue() and getValue() could support an optional parameter
indicating whether magic methods should be triggered (the default) or not.
Would you consider it?
Previous Comments:
------------------------------------------------------------------------
[2013-03-14 16:37:18] evan at digitalflophouse dot com
It would seem to me that the ideal solution here would be for
ReflectionProperty::setValue and ::getValue to support an optional parameter
indicating whether magic methods should be triggered (the default) or not.
------------------------------------------------------------------------
[2012-12-18 23:11:42] ocramius at gmail dot com
Isn't reflection able to handle properties in a different way from how userland code acts? I
wouldn't consider Reflection at the same level of other code, or at least I'm not aware of
any way of achieving the same functionality without having access to internal APIs.
I'm not denying the fact that this is probably a design issue and that it would require major
refactoring to workaround it, but the fact that such effort is needed doesn't mean that this is
not a bug.
Want to mark it as improvement instead? Or as a documentation issue and therefore language
limitation (with relative improvement issue)?
------------------------------------------------------------------------
[2012-12-18 21:15:18] stas@php.net
I am sorry, but I think your opinion is incorrect. Reflection works the same way and
uses the same engine methods as other ways of accessing properties. Making
Reflection work differently from the rest of the object model would require special
exceptions in all object engine for Reflection and seems to make little sense to me
in general. Reflection is just another way of doing the same thing as regular
property access. I am sorry that your code does not work as you wanted it to, and
you are welcome to propose ways to improve it, including participating in
getters/setters discussion ongoing, but I do not think in this particular case
reflection is doing something wrong. In any case, it is not a bug as the code is
doing exactly as the intent of the code was for it to do.
------------------------------------------------------------------------
[2012-12-10 02:04:46] ocramius at gmail dot com
@stas let me put it into a more "practical" use case I currently have:
I use reflection to populate instances of proxy objects that have "lazy" marked properties
unset at instantiation time. This is good to achieve lazy loading of public properties, but any r/w
access to the property itself via reflection triggers
__get or __set.
This makes it impossible to use reflection to populate the property. I worked around it by disabling
__get in particular situations (see https://github.com/Ocramius/common/blob/DCOM-96/lib/Doctrine/Common/Reflection/RuntimePublicReflectionProperty.php),
but at a terrible cost in performance terms and broken behaviour in rare cases.
Back to the issue itself:
I consider Reflection as my last resource to access and modify status of objects without affecting
anything else within my environment. Having reflection trigger any logic during an assignment is an
unwanted and dangerous behaviour in my opinion.
------------------------------------------------------------------------
[2012-12-10 01:51:32] stas@php.net
I'm not sure why you think this is what should be happening. When you unset
properties, they are no longer set. And when you access unset properties, magic
methods kick in. So where's the problem here?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=63463
--
Edit this bug report at https://bugs.php.net/bug.php?id=63463&edit=1