tags:

views:

318

answers:

7

what is the purpose of interface when writing a class?

heres an example i've seen online.

<?php
interface Chargeable {
    public function getPrice();
}

class Employee implements Chargeable {
    protected $price;

    public function getPrice() {
        return $this->price;
    }
}

$product = new Employee();

?>
+6  A: 

An interface is a concept in Object Oriented programming that enables polymorphism. Basically an interface is like a contract, that by which classes that implement it agree to provide certain functionality so that they can be used the same way other classes that use the interface

Your example shows classes that guarantee that they have the getPrice method available. You can then write code that take advantage of objects that have this method without worrying about what kind of class it is.

apphacker
+2  A: 

Please see this question.

Mitch Wheat
A: 

Interfaces allow you to separate the interface from implementation. This is handy when you want to have orthogonality in your code among other things.

Basically you will be able to create functions that accept a Chargeable and be able to pass any object in there as long as it implements Chargeable. This allows you to be flexible down the road if you need to change the Employee class. Also it allows your method to accept any object that is "chargeable".

Andrew Hare
+4  A: 

here is one os the ways how I learned about

imagine this scenario

abstract class Plane {

  public function openDoors();
}


interface Fliers {
  public function fly();
}

now lets use them

class Boing474 extends Plane implements Fliers {
  public function fly() {
    // some stuff
  }

  public function openDoors() {
    // do something
  }

}

and

class Tweety implements Fliers{
    public function fly() {
    // some stuff
  }
}

Boing747 is Plane that can fly and Tweety is bird than can fly but make no sense "openDoors". The point is that interfaces can be implemented by different kind of objects but classes can not. as you see Boing747 and Tweety hasn't nothing in common but both can fly.

Gabriel Sosa
perfect answer IMHO
gargantaun
I agree this is a very useful answer. Would love to see it edited for clarity.
Darren Newton
what you would like I edit?
Gabriel Sosa
A: 

In languages with multiple inheritance instead of interfaces you have abstract classes. In PHP there is no multiple inheritance, so you have interfaces. One class can implement various interfaces. The only point is to guarantee that your class has certain set of methods.

vartec
A: 

I'm struggling to grok this myself at the moment (and I think I'm reading the same book as the OP...).

It seems to me that interfaces simply enforce a "contractual" obligation on classes that implement the interface to implement functions/properties that appear in the interface. Yet, isn't it the case that classes/objects which implement the interface can do so in a unique fashion since the implementation is not defined?

There is no actual inheritance involved is there?

sunwukung
A: 

let me give you example to understand the the dimnation of this type of classes

public interface transport{
public double getSpeed(){return(0/*the speed*/);}
public void setSpeed(){return(0/*the speed*/);}


}   
class car extend transport{
//implementation of transport interface
public double getSpeed(){return(speed/*the speed*/);}
public void setSpeed(){speed=250;}
}
class train extend transport{
//implementation of transport interface 
....

}

class plane extend transport{
//implementation of transport interface 
....
}

so the interface class is the general concept .

hassan