PHP Designing Interfaces in PHP Introducing Interfaces Implementing an Interface

Colby Work
Colby Work
8,157 Points

So, in other words...

Implementing an interface forces certain methods to be added in a class? I am having trouble understanding the purpose of Interfaces, since no functionality is actually written within one. Why create an interface, rather than just add the methods into the class to begin with? I can see how they work, but not necessarily why

For example, if I have an interface with on/off methods, and have a TV, and a Radio class, that both implement that interface, I am still writing on/off methods for both classes, AND an extra step of writing an interface that is forcing me to write those on/off methods.

2 Answers

Am in the same shoes as you, really finding it difficult to understand why writing interfaces is important. Hope someone gets to explain this concept to me better

Glenré Charl Labuschagné
Glenré Charl Labuschagné
23,155 Points

My take is: you use interfaces to create a "template" for minimum required methods in class object(s). Thus enforcing certain rules/expectancies, that will error/alert you, if not met. Interfaces are normally used and implemented as a framework for others to use.