如果父类属性为get(http://php.net/manual/en/reflectionclass.getproperty.php#117476),则Reflection类会抛出ReflectionException,这是读取/写入Mock对象的私有属性的最佳方法吗?
这是受影响代码的简化版本:
class SomeClass{
private $some_value = false;
}
function setValue($class, $name, $value)
{
$reflection = new ReflectionClass($class);
$property = $reflection->getProperty($name);
$property->setAccessible(true);
$property->setValue($class, $value);
}
$mock = $this->getMockBuilder('SomeClass')->getMock();
setValue($mock, 'some_value', true);