可能重复:
How do I get a PHP class constructor to call its parent's parent's constructor
我知道这听起来很奇怪,但我正试图绕过一个bug。我怎么称呼祖父母方法?
<?php
class Person {
function speak(){ echo 'person'; }
}
class Child extends Person {
function speak(){ echo 'child'; }
}
class GrandChild extends Child {
function speak(){
//skip parent, in order to call grandparent speak method
}
}
答案 0 :(得分:14)
您可以明确地调用它;
class GrandChild extends Child {
function speak() {
Person::speak();
}
}
parent只是一种使用最近的基类而不在多个地方使用基类名的方法,但是给任何基类的类名也可以使用它而不是直接的父类
答案 1 :(得分:2)
PHP以本机方式执行此操作。
试试这个:
class Person {
function speak(){
echo 'person';
}
}
class Child extends Person {
function speak(){
echo 'child';
}
}
class GrandChild extends Child {
function speak() {
// Now here php allow you to call a parents method using this way.
// This is not a bug. I know it would make you think on a static methid, but
// notice that the function speak in the class Person is not a static function.
Person::speak();
}
}
$grandchild_object = new GrandChild();
$grandchild_object->speak();