获取php中父类的所有定义的类 [英] Get all defined classes of a parent class in php

查看:110
本文介绍了获取php中父类的所有定义的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class foo我需要确定所有文件都包含后, {
}
class boo extends foo {
}
class bar extends foo {
}

我想要获取一个数组:

  array('boo','bar'); 


解决方案

如果你需要的话, ,基类不应该知道这一点。



然而,如果你的定义已经包括(即你不需要包括新文件类,你可能你可以运行:

  $ children = array(); 
foreach(get_declared_classes()as $ class){
if($ class instanceof foo)$ children [] = $ class;
}


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆