在PHP中通过反射获取属性值
我有一门PHP课程在PHP中通过反射获取属性值,php,Php,我有一门PHP课程 class Test { private $name; public setName($name) { $this->name = $name; } public getName() { return this->name; } } 我想使用反射获取name属性的值 如何在PHP中实现这一点?简单如搜索:,请查看PHP文档和示例 获取ReflectionProperty实例后,只需执行getValue 可能重复的 $test = new Test();
class Test
{
private $name;
public setName($name)
{
$this->name = $name;
}
public getName()
{
return this->name;
}
}
我想使用反射获取name属性的值
如何在PHP中实现这一点?简单如搜索:,请查看PHP文档和示例
获取ReflectionProperty实例后,只需执行getValue
可能重复的
$test = new Test();
$test->setName('hello, world');
$reflection = new ReflectionClass($test);
$property = $reflection->getProperty('name');
$property->setAccessible(true);
var_dump($property->getValue($test));