Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ReflectionProperty::setValue() Deprecates A Wrong Object Type

ReflectionProperty::setValue() (and ReflectionProperty::setRawValue()) accept an object as the first argument to set the property on. Until PHP 8.6, passing an object that is not an instance of the class the property was declared in was silently accepted, and the property was simply created on that unrelated object. In PHP 8.6, doing so emits a deprecation notice.

PHP code

<?php

class A {
    public int $prop = 1;
}

class B {
    public int $prop = 0;
}

$rp = new ReflectionProperty(A::class, 'prop');
$rp->setValue(new B(), 42);
print "done\n";

?>

Before

done

After

PHP Deprecated:  Calling ReflectionProperty::setValue() with a given object that is not an instance of the class this property was declared in is deprecated in /codes/reflectionSetValueWrongObject.php on line 12

Deprecated: Calling ReflectionProperty::setValue() with a given object that is not an instance of the class this property was declared in is deprecated in /codes/reflectionSetValueWrongObject.php on line 12
done

PHP version change

This behavior was deprecated in 8.6.

This behavior changed in .

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat