Obtain information about a class

PHP5 includes interesting APIs to obtain information (list of members and methods) about the internal classes or interfaces.
Example of PHP5 interesting classes/interfaces: Exception, Iterator.
To obtain information about a class, instantiate the class ReflectionClass passing the name of the class we want information about. Now pass that instance to the static method export() of the class Reflection. The method print a list of detailed information about the class (see below the example):
Reflection::export(new ReflectionClass(‘Iterator’));
Interface [ interface Iterator implements Traversable ] {
– Methods [5] {
Method [ abstract public method current ] { }
Method [ abstract public method next ] { }
Method [ abstract public method key ] { }
Method [ abstract public method valid ] { }
Method [ abstract public method rewind ] { }
To obtain information about a PHP method, use the ReflectionFunction class, and the static method export() of the class Reflection.
In this example we’ll obtain information about the PHP function str_replace(). The export method show the parameters order, if required or not, and if is taken by value or reference.
Reflection::export(new ReflectionFunction(‘str_replace’));

Function [ function str_replace ] {
– Parameters [4] {
Parameter #0 [ $search ]
Parameter #1 [ $replace ]
Parameter #2 [ $subject ]
Parameter #3 [ &$replace_count ]
For user defined function, there are other interesting methods of the class ReflectionFunction. Here are some of them:
  • public bool isInternal()
  • public bool isUserDefined()
  • public string getFileName()
  • public int getStartLine()
  • public int getEndLine()
  • public string getDocComment()
  • public array getStaticVariables()
  • public mixed invoke([mixed args [, …]])
  • public mixed invokeArgs(array args)
  • public bool returnsReference()
  • public ReflectionParameter[] getParameters()
  • public int getNumberOfParameters()
  • public int getNumberOfRequiredParameters()
Similarly, there is a class to get information about the parameters: ReflectionParameter, which contain methods to retrieve arguments details (isArray(), getDefaultValue(), etc…).
Other APIs classes: ReflectionProperty (information about class properties) and ReflectionExtension (info about extensions)