HomePHP The Reflection API: Working with Reflected Methods
The Reflection API: Working with Reflected Methods
This third part of a seven-part series explores the methods of the PHP Reflection API. You will learn how to take advantage of their functionality to retrieve useful information about the methods defined by a reflected class.
One of today's most powerful approaches for retrieving information about the internal structure of classes and interfaces in PHP 5 is via the language's native reflection API. What's more, not only does this API comes packaged with a huge variety of methods that let developers perform all sorts of reverse-engineering tasks using an object-oriented approach, but it offers a flat learning curve.
In reality, knowing whether a method within a specified class is public, protected or private, or determining whether or not a declared property is static, only involves invoking the proper method provided by the API. The entire introspection process is that easy.
Naturally, if you've already read the two previous parts of this series, you're now pretty familiar with using the PHP reflection API to get basic data on a simple interface and its implementing class. In those two articles I demonstrated how to retrieve, for instance, the name, the default properties and constants of a sample reflected class. I also showed how to discover a few other minor details, such as its physical location within the file system.
As I said a moment ago, however, the API provides many other helpful methods for obtaining relevant information about a specific class. These include methods that allow you to know what methods are defined by the class, and their level of visibility.
Assuming that you're interested in learning more about this topic, in the lines to come I'm going to show you how to manipulate reflected methods in a painless way. This process will range from finding out if they're public, protected or private, to invoking them (when possible) directly from client code.
Now it's time to learn a few more useful things about the PHP reflection API. Let's go!