Set value on an attribute of a class that doesn't have setter


Dec 6, 2022 PHP


Useful to set the id of a doctrine entity for example in a test for example


$a = new A();
$reflection = new \ReflectionClass($a);
$property = $reflection->getProperty('privateProperty');
$property->setAccessible(true);
$property->setValue($a, 'new-value');

 

symfony
php
short