Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_class in static method and inheritance (php)

We have a code

class ParentClass {
  public static function getName() {
    return get_class(self);
  }
}

class ChildClass extends ParentClass {
}

echo ParentClass::getName(); # => 'ParentClass'
echo ChildClass::getName(); # => 'ParentClass'

if I use get_class($this) there is the same result. Also for self::$this, static::$this etc

Any way to get child class name without adding methods to child class for this?

like image 372
Alex Chrome Avatar asked Nov 25 '10 07:11

Alex Chrome


1 Answers

You'll have to use get_called_class, which binds late. Only available since PHP 5.3 though.

like image 171
deceze Avatar answered Oct 16 '22 01:10

deceze