How to skip execution of a parent method to execute grandparent method?

PHP has native way to do this.

try this:

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();

You can just call it explicitly;

class GrandChild extends Child {
    function speak() {
       Person::speak();
    }
}

parent is just a way to use the closest base class without using the base class name in more than one place, but giving any base class' class name works just as well to use that instead of the immediate parent.

Tags:

Php