1

As in interface only a method is specified without codes like

interface eat {

public function ways_of_eating_food($givingsomefood);

}

//and remaining class does is only access the method whats the use of it as they could manually create it in the class to 
class human implements eat {


public function ways_of_eating_food($foodtoeat){

This is how people eat the food//
}


}

class animal implements eat {

public function ways_of_eating_food($foodtoeat){
//this is how animal eat the food
}
}

As animal and human are two difference class the core part are part are same that is they do eat food given but the style is different so what actually is this useful and how can interface supports multiple inheritance

3
  • 3
    Possible duplicate of PHP Interfaces: How are they usable in practice? Commented Dec 10, 2015 at 18:50
  • 1
    PHP does support Multi-level inheritance, not multiple inheritance. That means you cannot have one class that inherit form 2 other classes. Commented Dec 10, 2015 at 19:04
  • hmmm... not sure if I agree with the duplicate. The theory is right, but the code example is not very explicit on how to actually use them. Commented Dec 11, 2015 at 2:00

2 Answers 2

1

Interfaces are useful for data hiding. Let's say you want to provide a class to some client code, but don't want to give them full access. One way to restrict access without limiting the functionality of your class is to implement an interface and require the client code to get objects through a factory.

Another benefit of interfaces is team development. If you have a dozen programmers working on different pieces of code that need to connect, it's best to define interfaces for the connections. As long as the programmers write according to the interfaces, everything will link together nicely regardless of personal choices and style.

Yet another benefit is that with interfaces you can use a class without having defined it first. For example, if you have a class that does a lot of intricate work and won't be finished before the rest of the project, the rest of the project can use an interface to it and avoid being stalled by the development of that one class.

Imagine you don`t really know what are the different ways living being can eat but you do not want other classes to not function before you discover all the possible eating methods. Just declare an interface eat and let other classes implement it.

source

Sign up to request clarification or add additional context in comments.

Comments

0

 they do eat food given but the style is different so what actually is this useful

You should read about type-hints. Interfaces are useful to manage objects that share behaviors, but you don't know before runtime which object you will have to use.

Consider a function that makes beings eat. Since you make an interface, you can type hint the interface in the function so it can manage any kind of eating beings:

function makeEat(eat $obj) { $obj->ways_of_eating_food( getFood() ); }
function getFood() { return $food; }

If you had not defined an interface however, you would have to make a function to make humans eat, another one to make cats eat, another one to make dogs eat, etc. This is really impractical. By using an interface and type-hinting it to the function, you can use the same function to do the same job.

how can interface supports multiple inheritance

As commented by Vincent:

PHP does support Multi-level inheritance, not multiple inheritance.

This means you can implement multiple different interfaces, but not extend multiple classes.

interface living { public function eat(food $food); }
interface food { public function getEnergyValue(); }
interface animal { public function breath(); }

class cow implements living, animal 
{
    private $energy = 0;
    private $secondsRemaining = 10;

    public function eat(food $food) { $this->energy += $food->getEnergyValue(); }
    public function breath() { $this->secondsRemaining += 10; }
}
class salad implements food 
{
    private $value = 50;

    public function getEnergyValue() { return $this->value; }
}

$bob = new cow();
$bob->eat(new salad());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.