比如说:classA{private$value=null;publicfunction__construct(){$this->value=1;}}classB{private$differentValue=null;publicfunction__construct(){$this->differentValue=1;}}A和B的作用完全不同,他们两个没有办法合并。classCextendsA{publicfunctiondisplay(){echo"OK";}}classDextendsB{publicfunctiondisplay(){echo"OK";}}C和D的内容又完全相同,这样的情况有没有什么好的方法把C和D的内容只写一遍?因为要向下兼容5.3的,所以trait不能用~~~
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
classA{private$value=null;publicfunction__construct(){$this->value=1;}}classB{private$differentValue=null;publicfunction__construct(){$this->differentValue=1;}}/***@methodvoiddisplay()*/classCextendsA{private$handler=null;publicfunction__construct(){parent::construct();$this->handler=newE();}publicfunction__call($name,$params){if(method_exists($this->handler,$name)){returncall_user_func_array(array($this->handler,$name),$params);}else{thrownewException('Method"'.$name.'"doesnotexists.');}}}/***@methodvoiddisplay()*/classDextendsB{private$handler=null;publicfunction__construct(){parent::construct();$this->handler=newE();}publicfunction__call($name,$params){if(method_exists($this->handler,$name)){returncall_user_func_array(array($this->handler,$name),$params);}else{thrownewException('Method"'.$name.'"doesnotexists.');}}}ClassE{publicfunctiondisplay(){echo"OK";}}
添加回答
举报
0/150
提交
取消