Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用类组合两个或多个函数_Php_Class - Fatal编程技术网

在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上声明了一个

我有两个php类

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很抱歉,你会注意到我在另一部分使用了正确的术语。