Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中通过反射获取属性值_Php - Fatal编程技术网

在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();

我有一门PHP课程

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));