一个人可以在PHP中输出类的完整继承链吗? [英] Can one output the full inheritance chain of a class in PHP?

查看:79
本文介绍了一个人可以在PHP中输出类的完整继承链吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给予

class a{...}
class b extends a{...}
class c extends b{...}
class d extends c{...}

是否有一种方法,可以从实例来表明其类定义扩展了c,扩展了b,扩展了a?给定类名,有没有办法静态地做到这一点?

Is there a way, from an instance of class d, to show that it's class definition extends c which extends b which extends a? Is there a way to do it statically given the class name?

我厌倦了从一个文件爬到另一个文件,弄清楚什么扩展了什么,依此类推.

I get tired of crawling from file to file figuring out what extends what, and so on.

推荐答案

我经常使用:

<?php
class grandfather {}
class father extends grandfather {}
class child extends father {}

function print_full_inheritance($class) {
  while ($class!==false) {
    echo $class . "\n";
    $class = get_parent_class($class);
  }
}

$child = new child();
print_full_inheritance(get_class($child));

?>

您可以在 http://php上的PHP手册中阅读更多内容. net/manual/en/function.get-parent-class.php .

这篇关于一个人可以在PHP中输出类的完整继承链吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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