You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ReflectionProperty inconsistently checks the instance type #17730
Methods of ReflectionProperty that accept an object instance, are checking the object type inconsistently.
Given a ReflectionProperty created with new ReflectionProperty($className, $propName), here is a breakdown of the checks performed by the different methods:
getValue(), isInitialized(): $object must be an instance of $className or of $propName's declaring class ($className itself or a parent)
getRawValue(), setRawValueWithoutLazyInitialization(): $object must be an instance of $className
setValue(), setRawValue(): $object must be an object
Technically, these methods would work with any object, except setRawValueWithoutLazyInitialization which accesses the property backing store directly. However it makes sense that methods of new ReflectionProperty($className, $propName) don't accept instances unrelated to $className.
Historically, the check in getValue() was added as part of https://bugs.php.net/bug.php?id=72209, and only accepted $className. It was then changed to accept the declaring class in c97b8bb, and both in 0e3045a, along with a TODO comment suggesting to accept only $className (but doing so would have been a BC). The check was propagated to getRawValue, setRawValueWithoutLazyInitialization with the TODO comment applied.
The inconsistency is not a huge issue, but it makes it impossible to cache the check result in #17698 (if the check were consistent, it would be possible to bypass the instanceof check when the cache is populated for the object's class).
Unfortunately, making the tests consistent would be a BC break:
Changing to "$object must be an instance of $className or of $propName's declaring class" would break https://3v4l.org/Tuej7
Changing to "$object must be an instance of $className" would break the same use-case, and also https://3v4l.org/E0srl
Activity
DanielEScherzer commentedon Jun 27, 2026
https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_reflectionpropertysetvalue_and_reflectionpropertysetrawvalue_with_wrong_types