[英]Get filename of child of the top of the class hierarchy, whatever level of inheritance
[英]Get top level class name when inheritance and class alias is used
我有一些以這種方式擴展的類:
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
所以最后我喜歡這個Residence extends Site1Residence extends Baseresidence extends CActiveRecord
在 Baseresidence 中,我有一個檢索實例的 static 方法model()
。
現在我可以打電話給::
$r=Residence::model();
問題是__CLASS__
常量被用作默認值,並且在該級別是 Baseresidence,我需要頂級 class 名稱(使用別名創建),它應該是'Residence'
如果我做:
echo get_class($r); // the Baseresidence is printed
目標是打印residence
調用$r=Residence::model();
我想從根本上解決它。
如何獲得該級別的頂級 class 名稱?
嘗試
get_called_class();
見http://php.net/manual/en/function.get-called-class.php
從文檔:
class foo {
static public function test() {
var_dump(get_called_class());
}
}
class bar extends foo {
}
foo::test();
bar::test();
上面的例子將 output:
string(3) "foo"
string(3) "bar"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.