With the reflection mechanism of PHP, Xin Xing implemented the plug-in.

Source: Internet
Author: User
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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.