I followed Xin Xing's PHP Reflection mechanism to implement the plug-in. the previous article in my blog explains how PHP Reflection is going. if the reader still does not know the reflection mechanism, you can search for or view my blog posts, which is a good choice. let's start to explain how to use PHP to implement the plug-in mechanism. The so-called plug-in mechanism means that we define an interface, that is, we define an interface, and then the third-party plug-in implements this interface. then we get this plug-in and call the function of this plug-in, we do not know the class name and other information of these plug-ins. we will use the reflection mechanism to implement this function.
Because I am just a simple example, the code I wrote is not long and very simple. Therefore, since you understand the above steps, you can directly read the source code, the comments are quite clear. if you have any questions, let me know and I will reply carefully.
ImplementsInterface ('ixin') {$ plugin [] = $ reclass;} return $ plugin ;} /*** call the msg method of the plug-in */function myexec () {$ arr = find (); foreach (find () as $ plugin) {// Determine whether the plug-in has a method msgif ($ plugin-> hasMethod ('MSG ')) {// Get an instance of this method class $ remethod = $ plugin-> getMethod ('MSG '); // if it is a static method, you can directly call if ($ remethod-> isStatic () {$ remethod-> invoke (null);} else {// declare an instance of the plug-in class first, then call it $ pluins = $ plugin-> newInstance (); $ remethod-> invoke ($ pluins );}}}} /*** you only need to call this function. * It automatically searches for all plug-ins and executes the msg function of the plug-in */myexec ();
Let me analyze it. the interface Ixin is our defined interface, and the class Xin is a third-party plug-in. We use find to automatically search for all the defined classes, then, we determine who inherits from Ixin these classes and implement the msg method. after obtaining this list, we can use the myexec method to call them.
Is the idea clear? Clear Question 1 .... O (distinct _ distinct) O ~