Get class name in static method

Is there some method in PHP to get the name of whatever class you’re currently in, when using only static methods? I’m thinking if there is, it probably has something to do with Reflection, but I’m not sure.

To illustrate, here’s what I like to do:

class MyClass
    public static function getName()
        // do something here, yielding the string variable $name
        return $name;

echo MyClass::getName();

This should output MyClass. Is there some way to do this? (This might seem silly, but that’s only because I’ve broken down what I need into an oversimplified example.)

Simpler than you think

public static function getName() {
       return __CLASS__;

However this won’t work if you extend MyClass.

class NewClass extends MyClass...
echo NewClass::getName(); // still says MyClass

You have to wait for php 5.3 and its “late static binding” feature for this to work properly. In the meantime, a hack based on [fphp]debug_backtrace[/fphp] seems to be the only choice.

Oh wow, I didn’t realize CLASS existed. That solves my problem; thank you!

Yeah, a function I have used before is:

	public static function getName(){
		foreach(debug_backtrace() as $arr){
			if($arr['function'] == 'getName'){
				return $arr['class'];
		return 'no';

there are also some similar variables, FILE, FUNCTION, and etc.

They’re called “Magic Constants.” Another useful one is LINE, which is good when you’re logging an error. Here’s their manual page:

A better solution might be to not use a static method. If you instantiate the class, you won’t have this problem.