Over the last year I have seen a lot of developers questioning the use of interfaces. Especially those which are used with older standards for creating code. Its important to understand that interfaces are here to help us. They are here to create abstraction and boundaries. It’s a lot easier to create code fast, clean and effective when you don’t need to worry about to much implementation details.
What is an interface?
An interface is a description of the actions that an object can do. An interface is a description of all the functions that an object must have to be an “X”. So if you want to make a light switch, then you need a function to turns it on and a function to turn it off. This are the public functions. In an interface we don’t care about the internals. Only how the object we are going to implement react with the outside world. An interface is a contract we have. A light switch always need to be turned on or off. That’s the contract. Every possible variation of a light switch need those 2 functions to be a light switch.
What is the purpose of an interface?
The purpose is to create loosely coupled software. This can be done by hiding implementations. With just creating an interface for a light switch we can start using that interface to create walls. We place the light switch on a wall, it does not matter what light switch. As long as it fits the interface. So this means you can create multiple implementations of a light switch without needing to worry if it will fit on the wall.
When creating an implementation of a class you then don’t need to take into account other classes that will use it. Because as long as you follow the interface your class will work on any place that uses the interface. This is why you should always depend on interfaces and not on implementations.
Interfaces also creates boundaries between the layers or components of our system. If you want to communicate between layers or components then you need to use interfaces. This way other layers our components don’t depend on implementations but on contracts of what each component or layer provides as public access. In the future I will go deeper about these boundaries.
What about multiple interfaces for the same class?
You can use multiple interfaces in PHP. This can be useful to add variations. Maybe we now need a light switch that can dim. Then we add a Dim interface with a function to dim. This way you can create an implementation with multiple interfaces. Its a light switch and it can be dimmed. For adding extra functionality to an implementation you can also use traits in PHP. But this is something for another time.
When is an interface overkill?
You don’t need to use an interface when your class does not need to create a boundary or a contract and when there is only ever one variation. In situations like this I also suggest to use the final keyword. This means your class is final and can not be extended. Any class that you want to extend should have an interface. A contract in how it should work and keep working.
Yes we should use interfaces in object oriented programming. And no we don’t need to use it for every class. You need to use it, when it makes sense. The most important step is to first understand what interfaces are and why you should use it. When designing according to the SOLID principles (which you should always do) then you will understand that interfaces are very important. Don’t just create code, but think first! Design and implement!