Php 5’s reflection api is one of its most powerful feature, which can be used for building robust, extensible applications. It provides built in functionality to analyze the inner structure of classes, their properties and methods and acquire the class metadata.We can perform a number of activities using reflection api, for example
- Instantiate, clone and export an object
- Get class name, parent class name, file name of class etc
- Get the constants, properties and methods in a class.
- Check whether the class or parent class is an interface or abstract class
- Check if a particular method, constant or property is defined in a class…. And much more
Plugin system using reflection api.
Suppose we need to develop a system that can be extended using plugin (Just like wordpress, but must be object oriented..!!).Most of the systems achieve this by implementing a number of hooks to which plugins can register during some initializations. We can do this using reflection api and some standard base functions. Here I will implement a simple application with a newsletter plugin that can attach its own menu to the main admin menu. Of course this is not a refactored code but can express the basic idea.
The soul of our application is a basic interface named Plugin which will define methods that must be implemented by client classes ie, plugins.
Next we will have our core application class which will load the components of our application. Before this we may need to identify the plugins required in the current page and initialize them. This class will have run-time capabilities that can modify the current behaviors.
Here I have created an instance of newsletter plugin to run this class. bindPlugin() function will identify the classes that implement Plugin interface. It will create a reflection instance of each plugin and add into an array. Function runEvent() perform somewhat similar job of apply_filter() function in wordpress. It can take a callback function and arguments to that function. runEvent() method will check if there is any plugin that have a particular method. If yes it wil invoke the function. This function make use of hasMothod() and getMethod(), which will return an object of ReflectionMethod class. This class provides a method ‘invoke()’, using which we can call the reflectionMethod object.
Now we can create a plugin. It must implement init() function that we have given in plugin interface. Inside this function we can have any plugin related initializations. Since we need to add an admin menu, we will implement adminMenu function which will return the menu for this plugin. In real time we will need to define a set of standard function that every plugin can implement.
And our index file will look somthing like this
You may say that most(probably all) of the above can be done without using reflection class. I agree with you…. But the beauty of reflection api lies in up to what extend we are using it. I will be explaining its advanced usages like writing a documenter class, exception class, etc in coming posts.