<?php
class MyParent {

    public static function tellSomething() {
        return __CLASS__;
    }
}

class MyChild extends MyParent {

}

echo MyChild::tellSomething();

The code above echos "MyParent". How can i get to name of child class - in this case "MyChild"? If it's possible...

I just simply need to know which child is calling the inherited method.

Accepted Answer

__CLASS__ is a pseudo-constant, that always refers to the class, where it is defined. With late-static-binding the function get_called_class() were introduced, that resolve the classname during runtime.

class MyParent {

  public static function tellSomething() {
    return get_called_class();
  }
}

class MyChild extends MyParent {

}

echo MyChild::tellSomething();

(as a sidenote: usually methods don't need to know the class on were they are called)

Written by KingCrunch
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki