If you want to build a PHP class browser or a PHP debugger, or programatically discover the nitty-gritty details about an object, reflection is a great new trick to teach to your old code.
The first beta version of PHP 5 was released in the summer of 2003 to much excitement. Developers descended upon PHP 5’s long list of object-oriented features, analyzing and experimenting with the nascent capabilities. Oddly, PHP 5’s Reflection application programming interface (API) was not part of that initial release, and was absent from the overview documentation. In the following weeks, the Reflection API quietly found its way into the PHP 5 source tree without fuss or fanfare. Yet its stealthy arrival belies its importance: PHP 5’s Reflection API is to PHP what the java.lang.reflect package is to Java.
The Reflection API consists of built-in classes for analyzing properties, methods, and classes. It’s similar in some respects to existing object functions such as get_class_vars(), but is more flexible and provides much greater detail. It’s also designed to work with PHP’s newest object-oriented features, such as access control, interfaces, and abstract classes.
If you want to build a class browser, a debugger, or just determine the class of an object, the Reflection API is a great new trick to teach to your old code.
Getting Started
Let’s take a good look at the features of the Reflection API and build a small example.
The Reflection API was not part of PHP 5 Beta 1, but is included in Beta 2 (and beyond). To use the API, update your software to the latest release of PHP 5.
The Reflection API consists of classes that model…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: