tags:

views:

84

answers:

3

Is it possible to get the name of the top level class from an extended class, without setting it from the top level class. See example below, I would like to get 'Foo' from Base. I know I could set a variable from Foo, but hoping to skip the extra step.

Thanks.

class Base {

 function __construct() {

  echo '<p>get_class: '.get_class().'</p>';
  echo '<p>__CLASS__: '.__CLASS__.'</p>';

 }

}


class Foo extends Base {

}


$test = new Foo();

(PHP 5.2.4+)

+2  A: 

get_called_class(); for static classes or get_class($this) for initiated.

get_called_class();, as Jason said, was introduce in PHP 5.3

Chacha102
`get_called_class()` is only available since 5.3
jason
I love PHP 5.3.
Chacha102
+3  A: 

Use:

get_class($this);
Ionuț G. Stan
Don't know who to credit this to. So I will pick the first one with the answer.
Louis W
A: 

You can simply use:

get_class($this);
Greg