Saddam
Saddam

Reputation: 1206

How to access parent's parent's methods from a en extended class?

I was studying the Inheritance concept in OOP in PHP as documented in php.net.

I came to know about various Features of inheritance in PHP like a class cannot inherit multiple classes and to overcome this we should use traits.

Similarly I came to a point which got my head banging. In PHP if we do multiple level inheritance like

class A {
        // more code here
}

class B extends A {
        // more code here
}

class C extends B {
        // more code here
}

class D extends C {
        // more code here
}

// and so on ........

so like this the classes can be further extended to multiple levels but I am wondering how we can call the top most parent class method from the bottom most child. I know we can use parent::methodName() to call the immediate parent function from child class but if there are in depth inheritance of n levels then how can we determine the parent class method from child class for example calling A::method from child D, and similarly call method of A from Z.

Upvotes: 0

Views: 183

Answers (1)

yivi
yivi

Reputation: 47380

You can't do this.

And attempting to do something like this would just show that there are problems in your inheritance model.

If a C extends B which extends A, whatever "original" methods of A are no longer relevant for C. If they are relevant, maybe one should have extended A in the first place.

Generally, try not to abuse inheritance too much. It's ripe with problems, and generally you should prefer composition over inheritance.

Upvotes: 1

Related Questions