HomePHP Reflecting Abstract Classes and Interface Implementers with the Reflection API
Reflecting Abstract Classes and Interface Implementers with the Reflection API
In this sixth part of a seven-part series, I explain how to use the power of reflection to find out if a class has been defined abstract and final, and if it implements a specified interface. Performing all of these tasks is a straightforward process, so you shouldn’t have major problems using these reflection methods within your own PHP applications.
When it comes to highlighting the most remarkable features incorporated into PHP 5 a few years ago, undoubtedly its enhanced object model, along with the support for native exceptions and interfaces, are the first things that come to mind. However, as you may have heard, the language also comes with a powerful library commonly known as the Reflection API, which allows developers to perform all sorts of reverse engineering tasks on both classes and interfaces. This process can be far more useful that you might think.
Nowadays, many of the methods included with the reflection API are used widely in well-established frameworks and third-party libraries. This has contributed to the spread of the API’s popularity among developers. It is possible, however, that you still haven’t had the opportunity to take advantage of the functionality given by reflection within your own object-oriented applications, or simply don’t know how to use this API in a really useful manner.
If that’s the case, in this group of tutorials you’ll find a decent number of code samples that will show you how to utilize some of the most important methods bundled with the reflection API. They will let you obtain relevant information about your classes and interfaces with only minor effort.
And speaking of relevant information on classes and interfaces, in the previous chapter of the series I discussed how to use the “hasProperty()” and “getStaticProperties()” reflection methods with a sample class. The first of these methods made it possible to know if a specific property was declared by the particular class, and the second one allowed you (at least in theory) to retrieve all of the class’s static properties in one go.
As I said before, the reflection API can be used for inspecting classes in many different ways. In the lines to come, in fact, I’m going to demonstrate how to employ it to determine if a specified class is concrete or abstract, and if it implements a particular interface. That sounds pretty interesting, right?
Now, it’s time to explore a few more handy methods packaged with the PHP reflection API. Let’s go!