迭代类的所有数据成员和函数的最佳方法是什么,并检查哪些是继承的
您必须使用反射来做到这一点。看来您必须手动查看所有父节点才能获得继承的属性。这是一个关于php.net的评论,可能是一个好的开始
复制代码,以防注释被删除…
function getClassProperties($className, $types='public'){
$ref = new ReflectionClass($className);
$props = $ref->getProperties();
$props_arr = array();
foreach($props as $prop){
$f = $prop->getName();
if($prop->isPublic() and (stripos($types, 'public') === FALSE)) continue;
if($prop->isPrivate() and (stripos($types, 'private') === FALSE)) continue;
if($prop->isProtected() and (stripos($types, 'protected') === FALSE)) continue;
if($prop->isStatic() and (stripos($types, 'static') === FALSE)) continue;
$props_arr[$f] = $prop;
}
if($parentClass = $ref->getParentClass()){
$parent_props_arr = getClassProperties($parentClass->getName());//RECURSION
if(count($parent_props_arr) > 0)
$props_arr = array_merge($parent_props_arr, $props_arr);
}
return $props_arr;
}
查看反射:http://php.net/manual/en/book.reflection.php
例如,你可以列出所有的公共和受保护的属性:
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
var_dump($props);
您可以使用ReflectionClass::getParentClass
获取父类,然后将您的类的属性与父类的属性进行比较,以查看继承的内容