HomePHP Getting Information on a Reflected Class with the Reflection API
Getting Information on a Reflected Class with the Reflection API
In this second part of a series, I explore some handy methods of the PHP Reflection API. They allow you to retrieve miscellaneous information about a class, including its name and containing file, as well as its starting and ending lines.
Overlooked by many developers, the Reflection API that comes with PHP 5 is a powerful reverse-engineering extension of the language that permits developers to analyze, inspect and retrieve valuable information about selected classes and interfaces by means of a set of convenient methods.
So, if you’re a PHP programmer interested in learning how to put this API to work for you, then this article series might be the material that you’re looking for. In its tutorials you’ll be provided with a decent number of code samples aimed at demonstrating how to use the most relevant reflective methods included with the API.
And now that you’ve been introduced to the goal of the series, it’s time to review the topics that were discussed in the first installment, taking into account the possibility that you haven’t read it yet. So, to put it simply, in that article I coded a couple of scripts that showed how to obtain the name of a reflected class and the values assigned to its unique constant via the “getName(),” getConstant()” and “getConstants()” reflection methods.
Despite their simplicity, these introductory examples demonstrated the remarkable introspective capabilities offered by the reflection API when used in an object-oriented environment. However, as you might have guessed, the API comes bundled with many other methods that deserve a close analysis as well.
With that premise in mind, in this second episode of the series I’m going to show you how to get miscellaneous information about a reflected class, including things like its starting and ending lines within its containing file, the interfaces that the class implements, and so forth.
Now, let’s leave the preliminaries behind us and continue to explore a few more handy methods provided by the PHP 5 reflection API. Let’s get going!