php - How to know the class who is calling a method on inherited class? -
i want know name of class calling method.
ex:
class mother{ static function foo(){ return "who call me"; } } class son extends mother{ } class otherson extends mother{ } son::foo(); >> son otherson::foo(); >> other son
how this?
found solution using get_called_class()
:
class mother{ static function foo(){ echo get_class(),php_eol; echo __class__,php_eol; echo get_called_class(),php_eol; } } class son1 extends mother {} class son2 extends mother {} son1::foo(); son2::foo();
returns:
mother mother son1 mother mother son2
so can see get_class
, __class__
both return mother
, using get_called_class()
return class called function!
looks use static::class
return same, if using php >= 5.5
Comments
Post a Comment