在PHP中使用类组合两个或多个函数
我有两个php类在PHP中使用类组合两个或多个函数,php,class,Php,Class,我有两个php类 Class classOne { private $stuff; public $stuff2; public function init(){ dosomestuff; } } & 当我调用函数init时 $obj = new classTwo(); $obj -> init(); #dosomeotherstuff PHP解释器将dosomeotherstuff正如任何人所期望的那样,因为classTwo类在方法init上声明了一个
Class classOne {
private $stuff;
public $stuff2;
public function init(){
dosomestuff;
}
}
&
当我调用函数init时
$obj = new classTwo();
$obj -> init(); #dosomeotherstuff
PHP解释器将dosomeotherstuff正如任何人所期望的那样,因为classTwo类在方法init上声明了一个重写
相反,有没有一种方法可以将第一个init和第二个init的效果结合起来,以获得类似的效果
$obj = new classTwo();
$obj -> init(); #dosomestuff, #dosomeotherstuff
非常感谢在重写函数中,您可以调用基本函数:
public function init() {
parent::init();
// your normal code
}
在重写函数中,可以调用基函数:
public function init() {
parent::init();
// your normal code
}
将父对象使用到子对象方法:
Class classTwo extends classOne {
private $stuff;
public $stuff2;
public function init(){ #This function is overloading the native classOne method init;
parent::init();
dosomeotherstuff;
}
}
将父对象使用到子对象方法:
Class classTwo extends classOne {
private $stuff;
public $stuff2;
public function init(){ #This function is overloading the native classOne method init;
parent::init();
dosomeotherstuff;
}
}
只需在seconde类init method中调用父类(parent::init();)的init methode即可。请做你的家庭作业。你终究是个好人。PS:正确的术语是方法重写,重载是另一个术语entirely@EliasVanOotegem很抱歉,您会注意到我在另一节中使用了正确的术语。只需在第二个类init method中调用父类的init methode(parent::init();)。请做你的家庭作业。你终究是个好人。PS:正确的术语是方法重写,重载是另一个术语entirely@EliasVanOotegem很抱歉,你会注意到我在另一部分使用了正确的术语。