如何從子實例中遞歸地調用覆蓋父函數?由於我沒有使用靜態方法,我不能使用parent::method()
,但必須做某種不同的事情。我有一種感覺,我的問題有更好的方法,可能是通過設計模式。這是我的階級結構:在php中調用覆蓋父函數的子函數
class Creature {
public function eat() {
// feed and digest
}
}
class Mammal extends Creature {
public function eat() {
// gather and chew
}
}
class Human extends Mammal {
public function eat() {
// have a meal
}
}
class WesternPerson extends Human {
public function eat() {
// use knife & fork
}
}
$me = new WesternPerson();
$me->eat();
調用eat()
應從特殊遞歸去向上到非常抽象類。最簡單的方法是,如果我可以在被覆蓋的函數中調用具有相同名稱的父函數。但我知道,它被覆蓋,所以我不能。
' parent :: method()'不是一個靜態調用(雖然它看起來像它)。只要使用那個就可以了。 – Wrikken
另請參閱:http://stackoverflow.com/a/16925581/2153758 – bwoebi
非常感謝!由於'::'運算符,我顯然感到困惑。我現在很清楚。 – quape