我有一个ReflectionProperty
对象使用以下代码:
$service = new ReflectionClass($this);
$properties = $service->getProperties();
$property = $properties[0];
我想从$property
变量中获得ReflectionObject
对象,以获得有关该属性的一些额外信息(例如它的类是什么以及它的类继承自哪些类)。我怎样才能做到这一点呢?
。
class A {
/**
* @var UtilsWebService
*/
public $prop;
}
现在,我得到了ReflectionClass(实际上是指'A'),我需要得到$prop
属性的ReflectionClass/ReflectionObject。我需要稍后检查$prop
的类型是什么,以及它扩展了哪些超类型。
谢谢
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());