RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Taking Advantage of the New Reflection API in PHP5

Discover how to use the Reflection API to extract information about PHP classes and execute class methods.

f you haven't yet upgraded from PHP4 to PHP5, this article will give you a serious reason to do so: PHP5 now supports reflection through the Reflection API, which you can use to examine variables, interfaces, functions, methods, parameters, classes—and more. The Reflection API is generous; it offers a large number of classes and methods that you can use to accomplish reflection tasks.

To get a sense of the the theoretical aspect of the Reflection API, you'll see a brief description of the Reflection API classes and methods, illustrated by short examples. Later, you'll see two real-world application examples in PHP that make run-time decisions about which methods to call and auto-generate well-formatted HTML documentation for your PHP classes.

Reflection API Classes and Methods
Figure 1. Reflection API Classes: Here are the relationships between the Reflection API classes and methods.
As a quick overview of the Reflection API's capabilities, here's a diagram (see Figure 1) that presents the Reflection API classes/methods and the relationships between them. Notice that all the Reflection API classes implement the Reflector interface except for Reflection, ReflectionException, and ReflectionFunctionAbstract. All exportable Reflection classes implement the Reflector interface.

The ReflectionClass
You can think of the ReflectionClass class as the main class of the Reflection API. You use it to apply reflection over other classes, extracting timely information about all class components. I'll show you a few examples of using ReflectionClass methods and then just list the calls and descriptions for the rest, because they're all similar.

  • public void construct(string name)—This is the ReflectionClass constructor.
  • public string getName()—This method returns the inspected class name.
   $class = new ReflectionClass('TestClass');
   echo "The class name: ".$class->getName();
  • public ReflectionMethod getConstructor()—This method returns a ReflectionMethod object that represents the introspected class constructor.
   $class = new ReflectionClass('TestClass');
   $constructor = $class->getConstructor();
   echo $constructor;
  • public ReflectionMethod getMethod(string name)—This method returns a ReflectionMethod object representing the method specified by the name parameter.
   $class = new ReflectionClass('TestClass');
   $method = $class->getMethod('testMethod_1');
   echo $method;
  • public ReflectionMethod[] getMethods()—Returns an array of RelectionMethod objects containing all the introspected class methods.
   $class = new ReflectionClass('TestClass');
   $methods = $class->getMethods();
   foreach($methods as $in)
      { echo $in; }
In addition to the methods already listed, the class exposes similar methods that you use in the same way: You create a ReflectionClass instance, passing the name of the class you want to inspect as a parameter, and then use that instance to call the methods below:

  • public ReflectionProperty getProperty(string name)
  • public ReflectionProperty[] getProperties()
  • public mixed getConstant(string name)
  • public array getConstants()
  • public ReflectionClass[] getInterfaces()
The objects you retrieve using the preceding methods give you access to "deeper" levels of information in the class, as described in the next section.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date