Php 如何列出对象的所有成员,并知道哪些是继承的成员
迭代类的所有数据成员和函数,并检查哪些是继承的,最好的方法是什么。看看反射: 例如,您可以列出所有公共和受保护的属性:Php 如何列出对象的所有成员,并知道哪些是继承的成员,php,Php,迭代类的所有数据成员和函数,并检查哪些是继承的,最好的方法是什么。看看反射: 例如,您可以列出所有公共和受保护的属性: $foo = new Foo(); $reflect = new ReflectionClass($foo); $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); var_dump($props); 您可以使用Refl
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
var_dump($props);
您可以使用ReflectionClass::getParentClass
获取父类,然后将类的属性与父类的属性进行比较,以查看继承的内容。您必须使用。看来您必须手动查看所有父级才能获得继承的属性。这是一个好的开始
复制了代码以防注释被删除
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;
}
看来我的问题已经解决了,但是你能解释一下这些带有continue关键字的if语句意味着什么吗?