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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -