tags:

views:

68

answers:

5

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 ?

+1  A: 

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.

Amber
Can you provide an example?
A: 

When should I use interfaces and why not use abstract classes instead ?

As soon as you use e.g the factory pattern an interface should be used. Im sure there are more examples for it.

Take a look at the various design patterns. http://www.ibm.com/developerworks/library/os-php-designptrns/

EDIT: changed link to a better explanation.

Rufinus
I don't think that the article you mention is very good =)
Jani Hartikainen
TBH i didnt read it, i use the PHP Design Pattern Book from Stephan Schmidt.
Rufinus
PHP and design patters are the reason I started this question. Most tutorials for design patterns related to php are not really on the point.
A: 

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.

Jani Hartikainen
A: 

Interface is very good practice in command development. It creates a integrity of program product. First time team writes interface, after abstract classes (with common methods and abstract methods).

Abstract class useful where we extends it in different classes. For example, method __construct() common for all child classes, but others methods are different.

Our team uses this model: Interface->Abstract->Class1->Class2

Class1 extends Abstract implements Interface

Class2 extends Abstract implements Interface
Alexander.Plutov
The question refers to a single developer. For a team I can understand your approach.
A: 

"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:

Gordon
Programming to an Interface and not an implementation in PHP is not really straight forward because PHP is weakly typed
I read Gof and a bunch of other book. Can you pls be more on point ?
@daniphp [that argument comes up every now and then](http://stackoverflow.com/questions/2758254/what-is-the-point-of-interfaces-in-a-weakly-typed-language-like-php). PHP is just *weakly* typed. It's not untyped.
Gordon