Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Parent - Fatal编程技术网

Php 在我背后默默地调用父方法

Php 在我背后默默地调用父方法,php,parent,Php,Parent,可能重复: 我有一节这样的课 abstract class theme{ abstract function header(){ // do stuff here } abstract function footer(){ // do stuff here } } 因此,所有子类都必须具有以下两种方法: class Atheme extends theme{ function header(){ // do stuff here

可能重复:

我有一节这样的课

abstract class theme{

  abstract function header(){
    // do stuff here
  }

  abstract function footer(){
    // do stuff here

  }

}
因此,所有子类都必须具有以下两种方法:

class Atheme extends theme{

  function header(){
    // do stuff here

    echo ..
  }

  function footer(){
    // do stuff here

    echo ..
  }

}
现在,当我调用其中一种方法时:

$atheme=新的atheme

$atheme->header()

我希望子类中的header()方法自动调用父类header()方法,而无需在子类中专门调用parent::header()


这可能吗?

如果您只有一个继承级别,您可以通过执行以下操作来完成此操作:

abstract class theme {
    final public function header() {
        // do parent class stuff
        $this->_header();
    }

    abstract protected function _header();

    // ditto for footer
}

class Atheme extends theme {
    protected function _header() {
        // do child class stuff
    }
}
如果继承链任意长,则需要使用