The default PHP abstract method method

I notice that when I define a method as abstract, the signature of the child classes must be compatible with it. This makes it impossible for me to use type checking in the signature of the child class.

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

In cases where there is a suitable default implementation, I tend to use the second approach. But can I hurt myself?

It seems strange to me that using β€œabstract” can be so restrictive, so I want to know if something is missing ...

Note. I see that similar questions are asked regarding other languages, but not so much PHP.

+5
source share
2 answers

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

If you do not respect interfaces, this creates a fragile design, because although by definition every child must support all the interfaces of their ancestors, you break it, bu define incompatible interfaces.

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

In addition, this is often a sign that you should prefer inheritance delegation.

+7
source

An interface (or abstract class) ensures consistency with your classes. This is the contract that you have between you and the class that you inherit. This sequence is key when several people write code and you want to reliably rearrange classes.

+4
source

All Articles