首页 > 代码库 > 继承和委托关系

继承和委托关系

小孩和公共设施

用继承的方法:

class Sanitation{    public function  Washhands(){        echo "Cleaning!";    }    }class Child extends  Sanitation{    }$child=new Child();$child->Washhands();

 

委托关系:

class Sanitation{    public function  Washhands(){        echo "Cleaning!";    }    }class Child  {    private $sanitation;    function __construct(){        $this->sanitation=new Sanitation();    }    public function Washhands(){        return $this->sanitation->Washhands();    }    }$child=new Child();$child->Washhands();