zoukankan      html  css  js  c++  java
  • PHP behavior 机制简单实现

    <?php
    class Base{
        private $_m = array();
        public function attachBehavior($behaviorObj){
                $behaviorObj->attach($this);
                $this->_m[] = $behaviorObj;
        }
    
        public function __call($method,$param){
           foreach($this->_m as $obj){
                if(method_exists($obj,$method)){
                     call_user_func(array($obj,$method),$param);
                }
           }
        }
    }
    
    class Behavior{
        protected $scope;
        public function attach($scope){
            $this->scope = $scope;
        }
    }
    
    class BehaviorTest extends Behavior{
    
       public function prints($param){
           print_r($this->scope);
       }
    }
    
    class TestObj extends Base{
       public function __construct(){
           $this->name = 'test';
           $this->age = 20;
       }
    }
    
    $behaviorTestIns = new BehaviorTest();
    $baseIns = new TestObj();
    $baseIns->attachBehavior($behaviorTestIns);
    $baseIns->prints();
    
    
    ?>
  • 相关阅读:
    node.js入门
    分布式爬虫
    ES6入门
    Vue.js入门
    用scrapy爬取亚马逊网站项目
    垃圾回收器
    HTTP协议详解
    有效的邮箱地址
    C#中正则表达式的使用
    抽象类
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/3240356.html
Copyright © 2011-2022 走看看