I would like to have a private property in a class, and be able to set it with another, via a ReflectionClass.
I know that if I create ReflectionProperties of the class' properties, I can set them to accessible, and then set their values.
However, if I set the property to accessible, does it become accessible everywhere (like a public property), or is it just in the context of the ReflectionProperty?
It will only be accessible through the ReflectionProperty::getValue()
and ReflectionProperty::setValue()
, so the original class and all its instances will not have their visibility changed.
Example:
<?php
class MyClass {
public function __construct() { $this->priv = 42; }
private $priv;
}
$a = new MyClass();
$ref = new ReflectionClass("MyClass");
$prop = $ref->getProperty("priv");
$prop->setAccessible(TRUE);
echo "priv: " . $prop->getValue($a) . "\n"; // OK!
echo $a->priv; // <-- error
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With