7

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

2
  • 1
    static is a keyword, not an object or an class. Why did you expect it to work? What were you intending to do? Commented Aug 21, 2011 at 4:25
  • I know I'm not suppose to expect PHP to treat classes and functions as first class citizens.. but I keep hoping.. >.> Commented Aug 21, 2011 at 13:57

1 Answer 1

24

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
Sign up to request clarification or add additional context in comments.

1 Comment

Used this for chaining of static methods when they are extended. E.g. public function method1() { return get_called_class(); }. Ugly, but works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.