Question

I notice that when I define a method as abstract, the signature of child classes needs to be compatible with it. This robs me of an opportunity to use the type checking in the child class' signature.

If I define the parent method as a concrete method with a default implementation, I can then override the parent method without complying with its interface.

In cases where a suitable default implementation exists, I am inclined to use the second approach. But am I letting myself in for trouble?

It just seems odd to me that the use of 'abstract' can be so limiting, so I want to know if I'm missing something...

Note - I see that similar questions have been asked in relation to other languages, but not so much PHP.

Was it helpful?

Solution

This is a very broad question, but in few words:

If you don't respect interfaces - it creates the fragile design, because even though by definition each child should support all the ancestor interfaces - you break it, bu defining incompatible interfaces.

There is a good law about that: http://en.wikipedia.org/wiki/Liskov_substitution_principle

Also, it's often a sign, that you should prefer delegation over inheritance.

OTHER TIPS

The interface (or abstract class) enforces consistency with your classes. It's a contract you have between you and the class you inherit from. This consistency is key when several people are writing code, and you want to interchange classes reliably.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top