1

I have created an interface Animal and make function makeSound. Now I implements Animal in Cat and Dog class and echo something from makeSound function. Now I am confused why I need to create interface while I do the same task only create Cat and Dog class. Let see an example

With Interface

interface Animal{
  public function makeSound();
}

class Cat implements Animal{
  public function makeSound(){
    echo 'Meow';
  }
}

class Dog implements Animal{
  public function makeSound(){
    echo 'Geow';
  }
}

$cat = new Cat();
$cat->makeSound();
echo "<br>";

$dog = new Dog();
$dog->makeSound();
echo "<br>";

Without Interface

 class Cat{
    public function makeSound(){
    echo 'Meow';
    }
}

 class Dog{
    public function makeSound(){
    echo 'Geow';
    }
 }

$cat = new Cat();
$cat->makeSound();
echo "<br>";

$dog = new Dog();
$dog->makeSound();
echo "<br>";
Nazmul Hoque
  • 761
  • 9
  • 9
  • 1
    It's not relates to php, interface is a contract that some class should respect, code is more readable and understandable. In your use case, interface animal describe behavior of "Animal", and all animals make sound apparently, so if you create `Class Tiger` it should implements `Animal` because it would `makeSound()` too. It's a way to provide good quality in development. – Lounis Oct 16 '21 at 17:35
  • 1
    Because you're not doing a task where an interface would be useful – ADyson Oct 16 '21 at 17:36
  • 1
    https://stackoverflow.com/questions/1686174/when-should-one-use-interfaces explains more – ADyson Oct 16 '21 at 17:37
  • 1
    https://www.culttt.com/2014/04/02/code-interface also has a good example – ADyson Oct 16 '21 at 17:37
  • 2
    @Lounis, have a look at the links provided by@ADyson, they might help you sharpen your understanding of interfaces, IMHO. – jibsteroos Oct 16 '21 at 17:50

1 Answers1

0

Now you can add this:

class Vet
{
    public function callAnimal(Animal $animal)
    {
        $animal->makeSound();
    }
}

$jimmy = new Vet();
$lulu = new Cat();
$fido = new Dog();
$jimmy->callAnimal($lulu);
$jimmy->callAnimal($fido);

If you don't have an interface, you'd have to write:

$jimmy->callCat($lulu);
$jimmy->callDot($fido);

... and modify your Vet for every new animal you create.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • This can be achieved by simply deriving the Dog and Cat classes from the Animal class (which doesn't have to implement any interface). IMHO, interfaces are only useful if you work in a team; blueprint of the minimum required functionality of the class. – Boris Jul 29 '22 at 12:40
  • @Boris Interfaces used to enable several distinct classes to share a probable group of properties and methods. The Interface is useful for abstracting a class where it specifies the operation performed by the class but does not reveal the background working or how it is going to do it. While inheritance assists the creation of specialized subclasses by base class and this is helpful in reusing the code. More generally inheritance is used to extend the class functionality. However, both are the concepts of object-oriented programming offer polymorphism feature. – Harry Feb 08 '23 at 14:42
  • @Harry I know, but thank you for clearing. Don't forget, if the method differs - there's always option to override it. Each level of abstraction helps us to ease development (easer understanding of the code logic, describing complex models, less mistakes and so on). If someone in the team created class based on the interface (even written by him/her self before) and didn't implemented all described methods - it will cause to error and that's great as he/she will immediately implement missing ones. That's why I mentioned the team case. – Boris Feb 12 '23 at 00:54