我有一个ReflectionProperty
对象使用以下代码:
$service = new \ReflectionClass($this);
$properties = $service->getProperties();
$property = $properties[0];
我想从ReflectionObject
变量中获取$property
对象以获取有关该属性的一些额外信息(例如它的类和它继承自哪个类)。我怎样才能做到这一点?
e.g。
class A {
/**
* @var \Utils\WebService
*/
public $prop;
}
现在,我得到了ReflectionClass(实际上是指' A'),我需要为$prop
属性获取ReflectionClass / ReflectionObject。我需要稍后检查$prop
的类型以及它扩展的超类型。
由于
答案 0 :(得分:0)
class WebCrawler {
}
class WebService extends WebCrawler {
}
class A {
/**
* @var \WebService
*/
public $prop;
public $variable = 'hello';
function __construct()
{
$this->prop = new WebService();
}
}
// reflection
$obj = new A();
$reflectionObject = new \ReflectionObject($obj);
// get all public and protected properties
$properties = $reflectionObject->getProperties(\ReflectionProperty::IS_PUBLIC);
$properties = \array_merge($properties, $reflectionObject->getProperties(\ReflectionProperty::IS_PROTECTED));
\var_dump($properties);
// create the array similar to what you asked for
$data = [];
foreach ($properties as $property) {
$name = $property->name;
$data[$name] = $obj->{$name};
}
\var_dump($data);
// get info which class was inherited to the class defined in A::prop property
$reflectedObjectFromProperty = new \ReflectionObject($data['prop']);
\var_dump($reflectedObjectFromProperty->getParentClass());