1. <?php 
  2.  
  3.  
  4. /** 
  5.  * php 混合器 ,间接实现 类的多继承 
  6.  * 
  7.  * @author sdm 
  8.  */ 
  9. class Qing_Mix { 
  10.  
  11.     private $objs = array(); 
  12.  
  13.     public function __construct($objs) { 
  14.         $this->objs = func_get_args(); 
  15.         foreach ($this->objs as &$obj) { 
  16.             $obj->mix = &$this
  17.         } 
  18.     } 
  19.  
  20.     public function __get($name) { 
  21.         foreach ($this->objs as &$obj) { 
  22.             if (property_exists($obj$name)) { 
  23.                 return $obj->$name
  24.             } 
  25.         } 
  26.     } 
  27.  
  28.     public function __set($name$value) { 
  29.         foreach ($this->objs as &$obj) { 
  30.             if (property_exists($obj$name)) { 
  31.                 return $obj->$name = $value
  32.             } 
  33.         } 
  34.     } 
  35.  
  36.     public function __isset($name) { 
  37.         foreach ($this->objs as &$obj) { 
  38.             if (property_exists($obj$name)) { 
  39.                 return true; 
  40.             } 
  41.         } 
  42.         return false; 
  43.     } 
  44.  
  45.     public function __call($name$arguments) { 
  46.         foreach ($this->objs as &$obj) { 
  47.             if (method_exists($obj$name)) { 
  48.                 return call_user_func_array(array($obj$name), $arguments); 
  49.             } 
  50.         } 
  51.     } 
  52.  
  53.  
  54. //测试 
  55. if (__FILE__ == realpath($_SERVER['SCRIPT_FILENAME'])) { 
  56.  
  57.     function Test_Qing_Mix() { 
  58.  
  59.         class Test_Qing_Mix_A { 
  60.  
  61.             public $a = 1; 
  62.  
  63.             function fun1() { 
  64.                 echo "fun1 called\n"
  65.             } 
  66.  
  67.         } 
  68.  
  69.         class Test_Qing_Mix_B { 
  70.  
  71.             public $b = 1; 
  72.  
  73.             function fun2() { 
  74.                 echo "fun2 called\n"
  75.             } 
  76.  
  77.             function fun3() { 
  78.                 echo "will call fun1 called\n"
  79.                 //调用当前混合器的 fun1 方法 
  80.                 $this->mix->fun1(); 
  81.             } 
  82.  
  83.         } 
  84.  
  85.         $mix = new Qing_Mix(new Test_Qing_Mix_A(), new Test_Qing_Mix_B()); 
  86.         echo "will call fun1\n"
  87.         //调用混合器的fun1 
  88.         $mix->fun1(); 
  89.  
  90.         echo "will call fun2\n"
  91.         $mix->fun2(); 
  92.  
  93.         $mix->fun3(); 
  94.     } 
  95.  
  96.     Test_Qing_Mix(); 

输出结果

 

will call fun1

fun1 called

will call fun2

fun2 called

will call fun1 called

fun1 called

 
参考:php-qing 框架