开发者社区> 问答> 正文

允许PHP应用程序使用插件的最佳方法

我正在用PHP启动一个新的Web应用程序,这次我想创建一些可以通过使用插件界面扩展的东西。 如何将“挂钩”写入其代码,以便插件可以附加到特定事件?

展开
收起
游客ufivfoddcd53c 2020-01-03 13:20:43 1083 0
1 条回答
写回答
取消 提交回答
  • 假设您不希望使用Observer模式,因为它要求您更改类方法以处理侦听任务,并需要通用的东西。假设您不想使用extends继承,因为您可能已经在类中继承了其他类。拥有一种通用的方法而无需花费太多精力就可以使任何类可插拔,这不是很好吗?就是这样:

    <?php
    
    ////////////////////
    // PART 1
    ////////////////////
    
    class Plugin {
    
        private $_RefObject;
        private $_Class = '';
    
        public function __construct(&$RefObject) {
            $this->_Class = get_class(&$RefObject);
            $this->_RefObject = $RefObject;
        }
    
        public function __set($sProperty,$mixed) {
            $sPlugin = $this->_Class . '_' . $sProperty . '_setEvent';
            if (is_callable($sPlugin)) {
                $mixed = call_user_func_array($sPlugin, $mixed);
            }   
            $this->_RefObject->$sProperty = $mixed;
        }
    
        public function __get($sProperty) {
            $asItems = (array) $this->_RefObject;
            $mixed = $asItems[$sProperty];
            $sPlugin = $this->_Class . '_' . $sProperty . '_getEvent';
            if (is_callable($sPlugin)) {
                $mixed = call_user_func_array($sPlugin, $mixed);
            }   
            return $mixed;
        }
    
        public function __call($sMethod,$mixed) {
            $sPlugin = $this->_Class . '_' .  $sMethod . '_beforeEvent';
            if (is_callable($sPlugin)) {
                $mixed = call_user_func_array($sPlugin, $mixed);
            }
            if ($mixed != 'BLOCK_EVENT') {
                call_user_func_array(array(&$this->_RefObject, $sMethod), $mixed);
                $sPlugin = $this->_Class . '_' . $sMethod . '_afterEvent';
                if (is_callable($sPlugin)) {
                    call_user_func_array($sPlugin, $mixed);
                }       
            } 
        }
    
    } //end class Plugin
    
    class Pluggable extends Plugin {
    } //end class Pluggable
    
    ////////////////////
    // PART 2
    ////////////////////
    
    class Dog {
    
        public $Name = '';
    
        public function bark(&$sHow) {
            echo "$sHow<br />\n";
        }
    
        public function sayName() {
            echo "<br />\nMy Name is: " . $this->Name . "<br />\n";
        }
    
    
    } //end class Dog
    
    $Dog = new Dog();
    
    ////////////////////
    // PART 3
    ////////////////////
    
    $PDog = new Pluggable($Dog);
    
    function Dog_bark_beforeEvent(&$mixed) {
        $mixed = 'Woof'; // Override saying 'meow' with 'Woof'
        //$mixed = 'BLOCK_EVENT'; // if you want to block the event
        return $mixed;
    }
    
    function Dog_bark_afterEvent(&$mixed) {
        echo $mixed; // show the override
    }
    
    function Dog_Name_setEvent(&$mixed) {
        $mixed = 'Coco'; // override 'Fido' with 'Coco'
        return $mixed;
    }
    
    function Dog_Name_getEvent(&$mixed) {
        $mixed = 'Different'; // override 'Coco' with 'Different'
        return $mixed;
    }
    
    ////////////////////
    // PART 4
    ////////////////////
    
    $PDog->Name = 'Fido';
    $PDog->Bark('meow');
    $PDog->SayName();
    echo 'My New Name is: ' . $PDog->Name;
    
    2020-01-03 13:29:29
    赞同 展开评论 打赏
问答分类:
PHP
问答地址:
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载