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!