Reflection in PHP 5
(Page 1 of 3 )
In this conclusion to a three-part series on advanced object-oriented programming features in PHP 5, you'll learn about reflection in detail. This article is excerpted from chapter 7 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
The classes used as examples in this and the previous chapters were for demonstrational purposes only, and therefore were simplistic enough that most of the features and behaviors could be examined at a single glance. However, real-world applications often require much more complex code. For instance, it isnít uncommon for a single application to consist of dozens of classes, with each class consisting of numerous members and complex methods. While opening the code in an editor does facilitate review, what if you just want to retrieve a list of all available classes, or all class methods or members for a specific class? Or perhaps youíd like to know the scope of a particular method (abstract, private, protected, public, or static). Sifting through the code to make such determinations can quickly grow tedious.
The idea of inspecting an object to learn more about it is known as introspection, whereas the process of actually doing so is called reflection. As of version 5, PHP offers a reflection API that is capable of querying not only classes and methods, but also functions, interfaces, and extensions. This section introduces reflection as applied to the review of classes and methods.
¶Tip The PHP manual offers more about the other features available to PHPís reflection API. Seehttp://www.php.net/oop5.reflectionfor more information.
As related to class and method introspection, the PHP reflection API consists of four classes:ReflectionClass,ReflectionMethod,ReflectionParameter, andReflectionProperty. Each class is introduced in turn in the following sections.
Writing the ReflectionClass Class
The ReflectionClass class is used to learn all about a class. It is capable of determining whether the class is a child class of some particular parent, retrieving a list of class methods and members, verifying whether the class is final, and much more. Listing 7-4 presents the ReflectionClass class contents. Although it isnít practical to introduce each of the more than 30 methods available to this class, the method names are fairly self-explanatory regarding their purpose. An example follows the listing.
Listing 7-4. The ReflectionClass Class
class ReflectionClass implements Reflector
public static string export()
public mixed getConstant(string name)
# The following three methods were introduced in PHP 5.1
public bool hasConstant(string name)
public bool implementsInterface(string name)
public bool isAbstract()
public stdclass newInstance(mixed* args)
public ReflectionExtension getExtension()
To seeReflectionClass in action, letís use it to examine thecorporatedrone class first created in Listing 7-1:
$class = new ReflectionClass("corporatedrone");
# Retrieve and output class methods
echo "Class methods: <br />";
foreach($methods as $method)
# Is the class abstract or final? echo "<br />"; ?> Executing this example returns the following output:
echo "<br />";
Executing this example returns the following output:
Is class corporatedrone Abstract: No