How to get the name of the class that caused the action in the parent class?

Example:

class ParentClass { function action() { echo $className; } } class ChildClass extends ParentClass {} $parentClass = new ParentClass(); $parentClass->action(); // ParentClass $childClass = new ChildClass(); $childClass->action(); // ChildClass 

    2 answers 2

    get_class :

     echo get_class($this); 

    for static - get_called_class ()

      There are so-called "Magic constants" , which can be read in the official manual .
      To solve your problem, determine the method in which you return __CLASS__ , or, as Etki wrote, get_class($this) .

       class ParentClass { function action() { return __CLASS__; } } 

      The method will return the full class name along with the namespace. To get only the class name, you can split the string by \ and take the last element.

       class ParentClass { function action() { $array = explode('\\', __CLASS__); return $array[count($array) - 1]; } }