Get top-level class name when using inheritance and class alias

I have several 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 { } 

and finally

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

So in the end I like it Residence extends Site1Residence extends Baseresidence extends CActiveRecord

In Baseresidence, I have a static model() method that retrieves an instance.

Now I can call ::

 $r=Residence::model(); 

The problem is that the __CLASS__ constant __CLASS__ used as the default value, and Baseresidence is at this level, and I need the top-level class name (created with an alias), and this should be "Residence"

if:

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

The goal is to type residence

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

How to get top level class name at this level?

0
source share
1 answer

Try

 get_called_class(); 

See http://php.net/manual/en/function.get-called-class.php

From the docs:

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

The above example outputs:

 string(3) "foo" string(3) "bar" 
+2
source

Source: https://habr.com/ru/post/908153/


All Articles