HomePHP Working with Reflected Properties with the Reflection API
Working with Reflected Properties with the Reflection API
In this fourth part of a seven-part series, I demonstrate how to use the PHP reflection API to retrieve useful information about the properties declared by a specific class. Reflection allows you to do more with properties of a class than determine their visibility or learn the values assigned to them, but that's what we'll be covering here.
In case you’re not aware of it, among the many enhancements and helpful features that were added to PHP 5, there’s one that has been unfairly overlooked by many developers. This might be because at first glance it doesn't seem to be as useful as the others. Yes, as this article’s title suggests, I’m talking about the reflection API, a powerful set of methods that permits you to perform an in-depth reverse engineering process on both classes and interfaces, in this way expanding the language’s introspective capabilities.
Fortunately, the API has progressively come out of the shadows of indifference, and nowadays its popularity has grown considerably. It's being used in a huge variety of scenarios and situations, ranging from creating low-level dispatcher classes in MVC-based frameworks, to building dependency injection containers.
Of course, it's possible that you still haven’t had the chance to explore this API in depth and discover what it can do for you. If that’s the case, in the course of this article series, I’m going to show you how to work with its most relevant methods, so you can start taking advantage of reflection within your own web applications.
I left off the previous tutorial explaining how to use the reflection API to get relevant information about the methods defined by a sample class, including the ability to learn if the methods in question were declared public, protected or private. However, the power of reflection doesn’t stop here; it allows you to analyze class properties with a similar level of introspection.
If this topic has caught your attention, in the following lines I’m going to code some friendly examples to demonstrate the functionality of the PHP reflection API when it comes to inspecting properties of a class.
Now, let’s get rid of the dull theory and learn more about using reflection in PHP 5. Let’s get going!