Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling parent method of inherited class from base class

The following example does not work because when parent is called in class A, php looks for the parent class of class A but it doesn't exist. I would rather this line to call Test() in class B.

Is this possible?

(I know this seems like a stupid example but it has a practical application)

abstract class A {
    function CallParentTest()
    {
        return call_parent_method('Test');
    }
}

abstract class B extends A {
    function Test()
    {
        return 'test passed';
    }
}

class C extends B {
    function Test()
    {
        return $this->CallParentTest();
    }
}

$object = new C();
echo $object->Test();

Thanks!

EDIT
I changed the parent keyword to the made up method call_parent_method because I think that may have been confusing people. I know there is no way to do this using the keyword.

Just as David Harkness pointed out, I am trying to implement the Template Method pattern but instead of using two different method names, I'm using one. B::Test() will be the default method unless substituted with alternate functionality.

like image 670
Godwin Avatar asked Jan 14 '12 00:01

Godwin


People also ask

How do you call an inheritance parent class?

Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions.

Can you call a parent class method from child class object?

Use the super keyword to access parent class method It should be used if the child class contains the same method as the parent class. In other words, the super keyword is used if the method is overridden. You override a method in the parent class by calling the same method in the child class.

Can we call derived class method from base class?

Even though the derived class can't call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that's what the Template Method pattern is all about.

Can a parent class inherit from a child class?

Inheritance concept is to inherit properties from one class to another but not vice versa. But since parent class reference variable points to sub class objects. So it is possible to access child class properties by parent class object if only the down casting is allowed or possible....


2 Answers

You can use reflection to bypass the natural calling order for overridden methods. In any context simply create a ReflectionMethod for the method you'd like to call and invoke it. You don't need to do this from the class itself, but you will need to call setAccessible(true) if the method isn't public.

class A {
    public function bypassOverride() {
        echo "Hi from A\n";
        $r = new ReflectionMethod('B', 'override');
        $r->invoke($this);
    }
}

class B extends A {
    public function override() {
        echo "Hi from B\n";
    }
}

class C extends B {
    public function override() {
        echo "Hi from C\n";
        $this->bypassOverride();
    }
}

$c = new C;
$c->override();

The output from this is

Hi from C
Hi from A
Hi from B

You could make bypassOverride() more generic and move it to a helper class if you need to do this a lot.

like image 67
David Harkness Avatar answered Sep 19 '22 12:09

David Harkness


Is this possible?

No.

It makes no sense to use the parent keyword except in child classes. It's only purpose is to be used by child classes to call methods that it as overridden. Think about multi-level parent calls where a child calls its parent's method of the same name and, in turn, that parent calls its parent's method of the same name.

like image 37
webbiedave Avatar answered Sep 19 '22 12:09

webbiedave