[php-src] master: [RFC] Deprecate `ReflectionProperty::setValue()` with wrong types

Daniel Scherzer <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: Daniel Scherzer (DanielEScherzer)
Date: 2026-08-12T10:09:25-07:00

Commit: https://github.com/php/php-src/commit/29f351d35542744c96837ae44e996785cc646b7e
Raw diff: https://github.com/php/php-src/commit/29f351d35542744c96837ae44e996785cc646b7e.diff

[RFC] Deprecate `ReflectionProperty::setValue()` with wrong types

https://wiki.php.net/rfc/deprecations_php_8_6

Changed paths:
  M  UPGRADING
  M  ext/reflection/php_reflection.c
  M  ext/reflection/tests/ReflectionProperty_setValue_error.phpt


Diff:

diff --git a/UPGRADING b/UPGRADING
index f82aad3f8b3c..34e3f62c5214 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -476,6 +476,11 @@ PHP 8.6 UPGRADE NOTES
   . The mysqli_get_charset() function is now deprecated.
     RFC: https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_mysqli_get_charset
 
+- Reflection:
+  . Calling ReflectionProperty::setValue() with an object that is not an
+    instance of the class on which the property was declared is now deprecated.
+    RFC: https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_reflectionpropertysetvalue_and_reflectionpropertysetrawvalue_with_wrong_types
+
 - SPL:
   . The spl_classes() function is now deprecated, use
     ReflectionExtension::getClassNames() instead.
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index 0b1c458db080..90fa559794c1 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -5919,6 +5919,15 @@ ZEND_METHOD(ReflectionProperty, setValue)
 			Z_PARAM_ZVAL(value)
 		ZEND_PARSE_PARAMETERS_END();
 
+		if (!instanceof_function(object->ce, intern->ce)) {
+			zend_string *method_name = get_active_function_or_method_name();
+			zend_error(E_DEPRECATED, "Calling %pS() with a given object that is not an instance of the class this property was declared in is deprecated", method_name);
+			zend_string_release(method_name);
+			if (UNEXPECTED(EG(exception))) {
+				RETURN_THROWS();
+			}
+		}
+
 		const zend_class_entry *old_scope = EG(fake_scope);
 		EG(fake_scope) = intern->ce;
 		object->handlers->write_property(object, ref->unmangled_name, value, ref->cache_slot);
diff --git a/ext/reflection/tests/ReflectionProperty_setValue_error.phpt b/ext/reflection/tests/ReflectionProperty_setValue_error.phpt
index 287a03679e61..25dd9adae2a9 100644
--- a/ext/reflection/tests/ReflectionProperty_setValue_error.phpt
+++ b/ext/reflection/tests/ReflectionProperty_setValue_error.phpt
@@ -30,11 +30,13 @@ $propInfo = new ReflectionProperty('TestClass', 'pub2');
 var_dump($propInfo->setValue($instanceWithNoProperties, "NewValue"));
 var_dump($instanceWithNoProperties->pub2);
 ?>
---EXPECT--
+--EXPECTF--
 Protected property:
 string(8) "NewValue"
 
 
 Instance without property:
+
+Deprecated: Calling ReflectionProperty::setValue() with a given object that is not an instance of the class this property was declared in is deprecated in %s on line %d
 NULL
 string(8) "NewValue"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.