Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionClass of static in PHP?

Tags:

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

like image 220
Pwnna Avatar asked Aug 21 '11 04:08

Pwnna


People also ask

What is meaning of static in PHP?

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

What is Reflectionclass PHP?

The reflection class is used to get information about the current state of the application. It's called reflection, because it looks at it's self, and can tell you information about the program your running, at run time.

What is the use of static class in PHP?

Static class is used for a single instance of that class whereas instantiated class is used when more than one instance is required. Static class contains static variables and static methods whereas instantiated class contains non-static variables and non-static methods.

What are static methods and properties in PHP?

Introduction to PHP static methods and properties PHP allows you to access the methods and properties in the context of a class rather than an object. Such methods and properties are class methods and properties. Class methods and class properties are called static methods and properties.


1 Answers

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
like image 183
Andrew Moore Avatar answered Sep 29 '22 01:09

Andrew Moore