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
}
}
如果继承链任意长,则需要使用