Pwnna
Pwnna

Reputation: 9538

ReflectionClass of static in PHP?

I'm aware that I cannot have new ReflectionClass(static) in PHP (5.3) as I just tried it. Is there any other way to get around this restriction?

Passing in a string is not an option, although somehow getting the string of the class name is acceptable. However, idk if it'd work as I'm working with namespaces as well.

Thanks

Upvotes: 7

Views: 5243

Answers (1)

Andrew Moore
Andrew Moore

Reputation: 95474

You can use get_called_class() to get a string containing the called class.

class Foo {
  public static function getReflection() {
    return new ReflectionClass(get_called_class());
  }
}

class Bar extends Foo {}

$reflectBar = Bar::getReflection();
// reflectBar now holds a ReflectionClass instance for Bar

Upvotes: 24

Related Questions