Follow the Sinsing with the PHP reflection mechanism to implement the plugin

Source: Internet
Author: User

The previous article of my blog explains how the reflection mechanism of PHP is going to be, if the reader is not clear about the reflection mechanism, you can search under or look at my blog post, is a good choice, we began to explain how to use PHP to implement the plug-in mechanism. The so-called plug-in mechanism is that we define an interface, that is, we define a interface, and then the third-party plug-in to implement the interface, and then we get the plug-in, to call the plug-in function, we can not know these plug-in class name and other information, We will then use the reflection mechanism to implement this function.

Because I am just a simple example, so I write the code is not long, and very simple, so, since we understand the above steps, then you can directly see the source code, the comments are quite clear, if the reader is not clear, you can tell me, I will seriously reply.

<?php/*** we define the good one interface, called Ixin*/interface ixin{function msg ();} /*** an enthusiastic developer wrote us a plugin class */class Xin implements Ixin{function msg () {echo "Sinsing hello";}} /*** We first search the plug-in class and determine if it implements the Msg method */function find () {//define an array that describes the plug-in $plugin = Array (); foreach (Get_declared_classes ()  as $class) {//Instantiate class $reclass for reflection class = new Reflectionclass ($class);//Determine if it implements interface Ixinif ($reclass->implementsinterface (' Ixin ') {$plugin [] = $reclass;}} return $plugin;} /*** we go to call the plug-in's Msg method */function Myexec () {$arr = find (); foreach (Find () as $plugin) {//Determine if 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, it can be called directly if ($remethod, IsStatic ()) {$remethod->invoke (null);} else{//first declares an instance of the plug-in class and then calls it $pluins = $plugin->newinstance (); $remethod->invoke ($pluins);}}} /*** We just need to call this function to do it. * It automatically searches for all plugins, and executes the MSG function of the plug-in */myexec ();

I'd like to analyze it, interface Ixin is the interface we define, and class Xin is a third-party plug-in, we use Find to automatically search all defined classes, and then we determine who inherits from Ixin in these classes, and implements the Msg method, we got this list since, You can use the Myexec method to invoke them.

Do you have a clear idea? Clear please call 1 .... O (∩_∩) o~

Follow the Sinsing with the PHP reflection mechanism to implement the plugin

Related Article

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.