在运行时确定对象的类层次结构 [英] Determining class hierarchy of an object at runtime

查看:68
本文介绍了在运行时确定对象的类层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

get_class()将给我对象的最终类.

get_class() will give me the eventual class of an object.

我想知道所有父类链.该怎么办?

I want to know all the chain of parent classes. How can this be done?

推荐答案

您可以反复调用 get_parent_class ,直到它返回 false :

function getClassHierarchy($object) {
    if (!is_object($object)) return false;
    $hierarchy = array();
    $class = get_class($object);
    do {
        $hierarchy[] = $class;
    } while (($class = get_parent_class($class)) !== false);
    return $hierarchy;
}

这篇关于在运行时确定对象的类层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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