12

I have always had a hard time understanding the real value of Interfaces when coding with Objects in PHP (could be other languages I imagine)

From what I understand you use an Interface to enforce or guarantee that when a Class is using an Interface that that class will have the methods defined in the Interface inside of that class.

So from my litte knowledge of using them, wouldn't that mean you would only find an Interface beneficial when defining more then 1 class that needs those Methods?

To be more clear, if I have a class that does one thing and no other classes need to do that kind of thing, then it would be pointless to use an Interface on that class?

So you wouldn't use an Interface on EVERY class you right?

PS) If you vote this question as exact duplicate then you didn't read the question and only the title as I have read most of the similar questions already

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 2
    This may be interesting in your case: http://stackoverflow.com/questions/20463/what-is-the-point-of-interfaces-in-php – Quasdunk Dec 18 '11 at 14:53
  • 3
    Yes, there's very little benefit to an interface if you know there's only ever going to be a single implementation of it. – Oliver Charlesworth Dec 18 '11 at 14:56
  • @Quasdunk that is one of the better reads about this topic, thanks – JasonDavis Dec 18 '11 at 15:04
  • I 'm also fairly certain that this has been asked before, but the referenced question ("Why use interfaces when we have abstract classes?") is not the correct dupe. – Jon Dec 18 '11 at 15:04
  • 1
    This one is more like it: http://stackoverflow.com/questions/240152/why-would-i-want-to-use-interfaces – Jon Dec 18 '11 at 15:06

1 Answers1

14

From what I understand you use an Interface to enforce or guarantee that when a Class is using an Interface that that class will have the methods defined in the Interface inside of that class.

This is actually only half of the deal (the technical part). There's also the all-important architectural half, which appears when you consume the interface and it goes like this:

function feed(IAnimal $interface) {
    // ...
}

(alternatively, a "factory" function that is documented to return an instance that implements IAnimal would also serve as an example).

The idea here is that the consumer of the interface says: "I want an animal to feed. I don't care if it flies, walks, or crawls. I don't care if it's big or small. I only care that it shares some features with all other animals" -- features that would comprise the definition of the interface.

In other words, interfaces serve to abstract the contract (interface) from the concrete implementation (classes). This gives implementers of concrete classes a free hand to modify, rename, remove and add implementations without breaking the code for users of the interface, something that is not possible if you are referencing concrete classes directly in your API.

As for the interface that is implemented by one class only: that's not enough information to decide. If there can plausibly be more implementations of the interface in the future, then it certainly does make sense (for example: an IHashFunction interface would make sense even if Sha1HashFunction were currently the only available implementation). Otherwise it doesn't offer anything.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks for taking the time to answer this, I know it seems this question is littered across SO but many answers are not that good. – JasonDavis Dec 18 '11 at 15:01
  • @jasondavis: That's a problem in SO unfortunately... and thank you. – Jon Dec 18 '11 at 15:05
  • 1
    I think of plugs and outlets. To work with an outlet, you have to know how to design your plug to work in the outlet (three prong, for instance, but there's a huge variety, including adaptors), but what you do with that plug once it leaves the wall is up to the device. Lots of variety comes out of a three (or two for my across-the-pond friends) prong plug, but at it's root it's still a three prong device. An abstract class could be seen as something hardwired into a system (networked smoke detector) but also somewhat independent in function. – Jared Farrish Dec 18 '11 at 15:15
  • 1
    Additionally it is of great benefit if you want to test your code using unit tests. – Dan Dec 18 '11 at 16:43