<?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();


?>

 

相关文章:

  • 2022-12-23
  • 2021-08-20
  • 2023-03-20
  • 2021-12-14
  • 2021-06-11
  • 2021-09-02
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
相关资源
相似解决方案