您应该将这两个类封装在一个包含的类中,并提供相关的接口(例如私有变量的setter / getters)
class YourFirstClass { public $variable; private $_variable2; public function setVariable2($a) { $this->_variable2 = $a; } } class YourSecondClass { public $variable; private $_variable2; public function setVariable2($a) { $this->_variable2 = $a; } } class ContaingClass { private $_first; private $_second; public function __construct(YourFirstClass $first,YourSecondClass $second) { $this->_first = $first; $this->_second = $second; } public function doSomething($aa) { $this->_first->setVariable2($aa); } }
研究(谷歌):“继承的构成”
脚注:抱歉非创意变量名称..