仅检索子类的属性 [英] Retrieving only properties of the child class
本文介绍了仅检索子类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似
class parent{
public $foo;
}
class child extends parent{
public $lol;
public function getFields()
{
return array_keys(get_class_vars(__CLASS__));
}
}
然后我得到一个包含子属性的数组,以...
and I get a array with the child properties in it to...
array('foo','lol');
是否有一种简单的解决方案仅从子类中获取属性?
is there a simple solution to get only the properties from the child class?
推荐答案
尝试这种方法(可能包含伪PHP代码:))
Try this approach (may contain pseudo PHP code :))
class parent{
public $foo;
public function getParentFields(){
return array_keys(get_class_vars(__CLASS__));
}
}
class child extends parent{
public $lol;
public function getFields()
{
$parentFields = parent::getParentFields();
$myfields = array_keys(get_class_vars(__CLASS__));
// just subtract parentFields from MyFields and you get the properties only exists on child
return the diff
}
}
使用parent :: getParentFields()函数确定哪些字段是父字段的想法.
The idea that using parent::getParentFields() function to determine which fields was parent fields.
这篇关于仅检索子类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文