+ 2

In PHP, What is interface? Why we use interface in class? What is the meaning of writing functions again in class?

Whey we use interface in php?

9th Sep 2020, 10:11 AM
Sajib Adhikary
Sajib Adhikary - avatar
1 Odpowiedź
+ 1
An interface defines behavior that must be implemented in several classes. For example, let's say in a program there is a class Human with a field `pet` which can either be a Cat, Dog or Cow. One way to solve this is to create an abstract super class PetAnimal that Cat, Dog, Cow can inherit from. The problem with this approach is that classes can only have 1 superclass and there is a tight coupling between abstract PetAnimal class and Human concrete class. What if the Human wanted an inanimate pet in future? Interfaces are a cleaner approach. You define only the necessary methods needed by the Human in the interface. Classes that implement the interface must declare those methods. https://code.sololearn.com/w3XrQoLV2u7Y
9th Sep 2020, 10:54 AM
Ore
Ore - avatar