Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a php interface redundant for apps build by a single developer?

Tags:

oop

php

interface

My question is : in PHP are interfaces really useful for developers that build website applications on their own ? Isn't an abstract class providing all the things an interface provides ?

If an interface is just a "contract" isn't the developer aware of the things the class should implement ?

The only single benefit I can think of is that a class can implement multiple interfaces but again how useful is this .. when you know everything a class should implement. You just force yourself to implement those methods.

As you can figure out I'm still waiting for that A-HA moment when I really understand why an interface is useful.

To wrap this up and put it simple : When should I use interfaces and why not use abstract classes instead ?

like image 841
johnlemon Avatar asked Aug 26 '10 07:08

johnlemon


3 Answers

Just because you "know" what something should implement, doesn't mean you remember it. It doesn't mean you'll never make a mistake and typo a function name. "Contracts" in programming aren't just for one developer to enforce things on another - they also let you provide a rigidity to the code that can catch mistakes that might otherwise slip under the radar.

like image 70
Amber Avatar answered Sep 19 '22 13:09

Amber


You can use interfaces for

  • Type-hinting in functions public function foo(IWhatever $x)
  • To check for type $x instanceof IWhatever
  • To create mock objects in unit tests $this->getMock('IWhatever')

Of course, you could use abstract classes too, but if you don't actually need to define any code it's probably a better idea to use an interface.

like image 35
Jani Hartikainen Avatar answered Sep 22 '22 13:09

Jani Hartikainen


"Programming to an Interface and not an implementation" is a principle introduced by the GoF in their books Design Patterns: Elements of Reusable Object-Oriented Software.

Quoting Erich Gamma on the principe:

Once you depend on interfaces only, you're decoupled from the implementation. That means the implementation can vary, and that's a healthy dependency relationship. For example, for testing purposes you can replace a heavy database implementation with a lighter-weight mock implementation. […]

So this approach gives you flexibility, but it also separates the really valuable part, the design, from the implementation, which allows clients to be decoupled from the implementation. One question is whether you should always use a Java interfaces for that. An abstract class is good as well. In fact, an abstract class gives you more flexibility when it comes to evolution. You can add new behavior without breaking clients. […]

In Java when you add a new method to an interface, you break all your clients. When you have an abstract class, you can add a new method and provide a default implementation in it. All the clients will continue to work. As always there is a trade-off, an interface gives you freedom with regard to the base class, an abstract class gives you the freedom to add new methods later. It isn't always possible to define an interface in an abstract class, but in the light of evolution you should consider whether an abstract class is sufficient.

Read the full interview here

So, You can use an interface or an abstract class. You just have to consider the trade-off. IMO, it's worth using interfaces, even if you are alone. You rarely know what your app will look in the end. The waterfall is a myth, so you will have to face change during development and interfaces make it easier to embrace it.

You might also be interested in:

  • What is the point of interfaces in a weakly-typed language like PHP?
  • Program to an interface not an implementation in php
  • why interfaces in dynamic/loosely-typed languages?
  • What is the point of interfaces in PHP?
  • Why is it possible to have an interface without a return type in PHP?

and some more:

  • https://stackoverflow.com/search?q=weakly+typed+interfaces+php
like image 27
Gordon Avatar answered Sep 20 '22 13:09

Gordon