I'm having difficulty writing to class properties using the Reflection API. $p->setValue($r, $value)
throws an exception saying that the property is read only, however, PHP (to my knowledge) has no read only class properties (only methods using final
), so what's the crack here?
The problem code:
public function apply($source, $target) {
$r = new \ReflectionObject($target);
foreach ($source as $key => $value) {
$p = $r->getProperty($this->entries[$key]);
$p->setAccessible(true);
$p->setValue($r, $value); // <--- problem here
}
}
The exception reads as follows:
Uncaught ReflectionException: Cannot set read-only property ReflectionObject::$name
An excerpt from the class being reflected ($target
in the above snippet), this issue remains whether I set the properties to public
or keep private
private $id;
private $name;
Nothing special going on with this class, just a plain object with a default constructor.
Is there something in the docs that I've missed, or is this just PHP weirdness that I need to accept?
Using version 7.1
Aucun commentaire:
Enregistrer un commentaire