在php中获取父类的所有定义的类 [英] Get all defined classes of a parent class in php
本文介绍了在php中获取父类的所有定义的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在包含所有文件后,我需要确定哪些类扩展了父类,因此:
I need to determine, after all files have been included, which classes extend a parent class, so:
class foo{
}
class boo extends foo{
}
class bar extends foo{
}
并且我希望能够获取一个数组,例如:
and I'd like to be able to grab an array like:
array('boo','bar');
推荐答案
如果你需要它,它真的闻起来像糟糕的代码,基类不应该需要知道这一点.
If you need that, it really smells like bad code, the base class shouldn't need to know this.
但是,如果您的定义已被包含(即您不需要包含您可能拥有的类的新文件),您可以运行:
However, if you definitions have been included (i.e. you don't need to include new files with classes you possibly have), you could run:
$children = array();
foreach(get_declared_classes() as $class){
if($class instanceof foo) $children[] = $class;
}
这篇关于在php中获取父类的所有定义的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文