ReflectionProperty::setValue
プロパティの値を設定する
&reftitle.description;
public voidReflectionProperty::setValue
objectnullobject
mixedvalue
public voidReflectionProperty::setValue
mixedvalue
プロパティの値を設定 (変更) します。
static プロパティの値を設定する場合は、
ReflectionProperty::setValue(null, $value)
を使いましょう。
&reftitle.parameters;
object
staticプロパティ の場合、&null; を渡してください。
static でないプロパティの場合は、オブジェクトを渡してください。
value
新しい値。
&reftitle.returnvalues;
&return.void;
&reftitle.changelog;
&Version;
&Description;
8.3.0
このメソッドにひとつだけ引数を渡してコールすることは、推奨されなくなりました。static プロパティの場合は、代わりに ReflectionProperty::setValue(null, $value) を使ってください。
8.1.0
private と protected なプロパティは
ReflectionProperty::setValue
ですぐにアクセスできるようになりました。
これより前のバージョンでは、
ReflectionProperty::setAccessible
をコールすることでアクセスできるようにする必要がありました。
そうしない場合、
ReflectionException
がスローされていました。
&reftitle.examples;
ReflectionProperty::setValue の例
getProperty('staticProperty');
$reflectionProperty->setValue(null, 'foo');
var_dump(Foo::$staticProperty);
$foo = new Foo;
$reflectionClass->getProperty('property')->setValue($foo, 'bar');
var_dump($foo->property);
$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true); // PHP 8.1.0 より前のバージョンのみ、この行の実行が必須でした
$reflectionProperty->setValue($foo, 'foobar');
var_dump($reflectionProperty->getValue($foo));
?>
]]>
&example.outputs;
&reftitle.seealso;
ReflectionProperty::getValue
ReflectionProperty::setAccessible
ReflectionClass::setStaticPropertyValue