1

Possible Duplicate:
Interface vs Abstract Class (general OO)
Interface or an Abstract Class: which one to use?

I had a few questions about interfaces in PHP :

If interface works as a kind of blueprint for classes that implement it, why not use abstract parent classes instead?

In what cases would it be more proper to use a interface instead of an abstract class?

In what cases would it be more proper to use an abstract class instead of a interface?

What exactly is usage of interface?

Community
  • 1
  • 1
Peeyush Kushwaha
  • 3,453
  • 8
  • 35
  • 69
  • 3
    Or one more specific to PHP: http://stackoverflow.com/questions/1814821/interface-or-an-abstract-class-which-one-to-use – cmbuckley Jan 11 '13 at 11:06
  • This is a fairly generic OOP concept; no need to restrict your research to PHP. – SDC Jan 11 '13 at 11:20

1 Answers1

3

A big difference is that you cannot extend multiple abstract classes, but you can implement multiple interfaces.

So in this way, objects can have multiple purposes and be used in various different scenarios if they implement multiple interfaces.

So to make this more tangible...

interface Wine{
   function isPeppery();
}

interface Dessert{
   function hasBerries(){
}

class PortWine implements Wine, Dessert{
   function isPeppery(){
     returns false;
   }
   function hasBerries(){
     returns true;
   }
}

class BlackForestCake implements Dessert{
   function hasBerries(){
      returns true;
   }
 }

In this way, you can have objects that serve multiple purposes throughout your application.

Kaiesh
  • 1,042
  • 2
  • 14
  • 21