HomePHP Getting More Info on Reflected Properties with the Reflection API
Getting More Info on Reflected Properties with the Reflection API
In this fifth part of the series you will learn a couple of additional methods included with the PHP reflection API. The first one will allow you to check the existence of a specified class property, and the second one will permit you to retrieve all of the static properties defined by that class at once.
Frankly speaking, the powerful reflection library that comes included with PHP 5 is one of those features that you, as a web developer, can’t live without once you’ve discovered its real functionality. While is fair to say that in the old days of PHP 4, the language already provided some native functions that allowed developers to achieve a decent level of introspection in classes and objects, the reflection API permits you to analyze the internal structure of classes and interfaces by using an intuitive object-oriented approach, or expressed in other words, you can use reflective classes that inspect other classes. What else can you ask for?
Well, feature-hungry programmers (and that includes myself, of course) will always ask for more. In its current version, however, the reflection API will let you perform a thorough reverse engineering process on your own classes, such as knowing what methods and properties they define, the level of visibility assigned to those members, and even instantiate directly classes from client code without having to explicitly write any “new” constructs.
Naturally, if you’ve been a loyal follower of this series and already went through all of the articles that precede this one, then you're already familiar with using some of the most important methods included with the reflection API. In those tutorials I coded a few simple examples that showed, for instance, how to get all of the methods declared by a class and how to determine if the methods in question were public, protected or private.
Also, I left off the last installment discussing how to retrieve all of the properties declared by the aforementioned class, along with their level of visibility, which was a straightforward process. Of course, as you might have guessed, the ability to manipulate class properties via reflection doesn’t stop here. To prove this, in the lines to come I’m going to demonstrate how to determine if a specified property exists within a class, and whether or not the class defines any static properties.
So, now that you know what to expect from this part of the series, it’s time to explore a few more handy methods bundled with the PHP reflection API. Let’s jump in!