|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-05-12 15:37 UTC] ocramius at gmail dot com
Description: ------------ `ReflectionProperty#getValue($object)` does not check whether the given `$object` matches the expected type. If `$r = ReflectionProperty(Foo::class, 'bar')`, then only instances of `Foo` should be allowed by `ReflectionProperty#getValue()` and `ReflectionProperty#setValue()`. All other values should cause an exception to be raised. As a reference, `ReflectionMethod` behaves correctly ( https://3v4l.org/U2u0h ), since `ReflectionMethod#invoke($object)` and `ReflectionMethod#invokeArgs($object)` reject incompatible `$object` instances. Test script: --------------- <?php class Foo { public $bar = __LINE__; } class Baz { public $bar = __LINE__; } var_dump((new ReflectionProperty(Foo::class, 'bar'))->getValue(new Baz())); Expected result: ---------------- Fatal error: Uncaught ReflectionException: Expected an instance of Foo, got 'Baz' instead does not exist in /.../test-script.php Actual result: -------------- (int) 10 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 21:00:01 2025 UTC |
This can't be fixed without changing a test, and the test doesn't make any sense at all. The test seems to imply that what you are seeing is expected behaviour ... Here's a patch: diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 4c3f624..ae1e932 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -5382,6 +5382,11 @@ ZEND_METHOD(reflection_property, getValue) return; } + if (!instanceof_function(Z_OBJCE_P(object), ref->ce)) { + _DO_THROW("Given object is not an instance of the class this property was declared in"); + /* Returns from this function */ + } + zend_unmangle_property_name_ex(ref->prop.name, &class_name, &prop_name, &prop_name_len); member_p = zend_read_property(ref->ce, object, prop_name, prop_name_len, 0, &rv); if (member_p != &rv) { This is the stupid test (last test in ReflectionProperty_getValue_error.phpt): echo "\n\nInstance without property:\n"; $propInfo = new ReflectionProperty('TestClass', 'pub2'); var_dump($propInfo->getValue($instanceWithNoProperties));