开发者

ReflectionClass of static in PHP?

开发者 https://www.devze.com 2023-03-29 08:10 出处:网络
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?

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.

开发者_如何学JAVA

Thanks


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
0

精彩评论

暂无评论...
验证码 换一张
取 消