使用继承和类别名时获取顶级类名称 [英] Get top level class name when inheritance and class alias is used

查看:46
本文介绍了使用继承和类别名时获取顶级类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这种方式扩展了一些课程:

I have some classes extended this way:

class Baseresidence extends CActiveRecord {
    public static function model($className=__CLASS__) {
        return parent::model($className); // framework needs, can't modify
    }    
}

class Site1Residence extends Baseresidence {

}

最后

class_alias('Site1Residence', 'Residence'); // this is part of an autoloader

所以最后我有了这样的代码住宅扩展了Site1Residence扩展了Baseresidence扩展了CActiveRecord

So in the end I have like this Residence extends Site1Residence extends Baseresidence extends CActiveRecord

在Baseresidence中,我有一个静态方法 model()来检索实例.

In the Baseresidence I have a static method model() which retrieves an instance.

现在我可以打电话给::

Now I can call::

$r=Residence::model();

问题是 __ CLASS __ 常量用作默认值,并且在该级别上是Baseresidence,我在那里需要顶层类名(使用别名创建),并且应该为'居住地"

The problem is that __CLASS__ constant is used as default value, and that on that level is Baseresidence, and I need there the top level class name (created with the alias) and it should be 'Residence'

如果我这样做:

echo get_class($r); // the Baseresidence is printed

目标是打印住所

在调用 $ r = Residence :: model(); 时,我不想传递任何内容.

I do not want to pass anything when calling $r=Residence::model(); I would like to resolve it on the roots.

如何获取该级别的顶级班级名称?

How to get the top level class name on that level?

推荐答案

尝试

get_called_class();

请参见 http://php.net/manual/en/function.get-drawn-class.php

从文档中

class foo {
    static public function test() {
        var_dump(get_called_class());
    }
}

class bar extends foo {
}

foo::test();
bar::test();

上面的示例将输出:

string(3) "foo"
string(3) "bar"

这篇关于使用继承和类别名时获取顶级类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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