045 using the reflection mechanism, simple to implement PHP plug-in mode

<? PHP
 // use reflection, simple to implement PHP plug-in mode 
#     assume that we have an open-source product, all developers have to be on top of my custom needs, secondary development, 
#     new module was developed after completion It is a not the same as a new plug-in, can be placed in a specific location automatically loaded 

# this is our open source 
    interface Demo {
         # all plug-ins must implement this method 
        public  function msg (); 
    } 
    
    # this is Xiao Ming to develop plug-ins -1 
    class Xiaoming the implements Demo {
         public  function MSG () {
             echo 'Bob: I silently look at you loaded to force a' ; 
        } 
    } 
    
    # this is Li plug-ins developed -2 
    class Xiaoli the implements{Demo
         public  function msg () {
             echo 'Mike: I loaded to force, you could get me drop yesterday? a ' ; 
        } 
    } 
    
    # we first search for the plug-in classes, and determines whether or not it implements the method msg 
    function Find () {
         # definition describes widget array (one example) 
        $ plugin = Array (); 
    
        the foreach ( get_declared_classes () AS   $ class ) {
             $ Reclass = new new the ReflectionClass ( $ class ); 
    
            # detect whether the class inheritance and interfaces Demo 
            iF ( $ Reclass -> implementsInterface ( 'Demo' )) {
                $ plugin [] = $ Reclass ; 
            } 
        } 
        return  $ plugin ; 
    } 

/ * * 
 * write a listening msg Methods All plugins corresponding function 
 * / 
    function myexec () {
         the foreach (Find () AS  $ plugin ) {
             # determines the whether the plug has msg method of 
            IF ( $ plugin -> hasMethod ( 'msg' )) {
                 # give one example of this method of the class 
                $ remethod = $ plugin -> getMethod ( 'msg' );
                 # if it is a static method, directly call to 
                IF ( $ remethod-> the isStatic ()) {
                     $ remethod -> Invoke ( null ); 
                } the else {
                     # one example declare plugin class, and then call it 
                    $ pluins = $ plugin -> the newInstance ();
                     $ remethod -> Invoke ( $ pluins ); 
                } 
            } 
        } 
    } 

# to listen to all plug-ins 
myexec ();
 ?>

 

Guess you like

Origin www.cnblogs.com/tianpan2019/p/10994177.html