Php 如何跳过父方法的执行以执行祖父母方法?
可能重复:Php 如何跳过父方法的执行以执行祖父母方法?,php,Php,可能重复: 我知道这听起来很奇怪,但我想避开一只虫子。我怎样才能称之为祖父母方法 <?php class Person { function speak(){ echo 'person'; } } class Child extends Person { function speak(){ echo 'child'; } } class GrandChild extends Child { function speak(){ //skip parent
我知道这听起来很奇怪,但我想避开一只虫子。我怎样才能称之为祖父母方法
<?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
}
}
您可以显式地调用它
class GrandChild extends Child {
function speak() {
Person::speak();
}
}
这只是一种使用最接近的基类的方法,不需要在多个位置使用基类名称,但给出任何基类的类名也可以使用它而不是直接的父类。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();
您可以控制层次结构中的所有类吗?