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 ?
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.
You can use interfaces for
public function foo(IWhatever $x)
$x instanceof IWhatever
$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.
"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:
and some more:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With