PHP Interfaces

PHP Interfaces


PHP - What are Interfaces?

A interface is used to make collection of methods into a groups and to support multiple inheritence.

You can specify which methods a class should implement by using interfaces definitions.

Interfaces make it simple to use multiple classes at the same time. "Polymorphism" is a term that refers to when two or more classes share the same interfaces.

The interface keyword is used to declare interfaces in a program :

Syntax :-

<?php
interface InterfaceName {
  public function someMethod1();
  public function someMethod2($name, $color);
  public function someMethod3() : string;
}
?>
You can also search for these topics, What are php oop Interfaces ?, use of classes in php oop interfaces, php interface keywords, Syntax for php oop interfaces, define polymorphism in php oop.

PHP - Interfaces vs. Abstract Classes

Abstract classes and interfaces are comparable. The distinction between interfaces and abstract classes is as follows :

  • In contrast, abstract classes can have characteristics.
  • Abstract class methods are either public or protected, while interface methods are required to be public accessible.
  • It is not essential to use the keyword abstract because all methods in an interface are abstract.
  • Inheritance from one class and implementation of an interface are both possible.


You can also search for these topics, PHP Interfaces vs. Abstract Classes, php difference between interfaces and abstract classes, how to implement a code php, use of abstract keywords in php, define the abstract vs interface properties.

PHP - Using Interfaces

A class must employ the implements keyword in order to implement an interface.

All of the methods of an interface must be implemented by a class that implements it.

Example 1 :-

<!DOCTYPE html>
<html>
<body>
<?php
interface Animal {
  public function makeSound();
}
class Cat implements Animal {
  public function makeSound() {
    echo "Meow";
  }
}
$animal = new Cat();
$animal->makeSound();
?>
 
</body>
</html>

Output :-

Meow

Assume we want to create software that handles a group of animals based on the preceding example. There are some actions that all of the animals can perform, but each species executes it in its own unique way.

Example 2 :- We can develop code that works for all of the animals, even if they behave differently, by using interfaces :

<!DOCTYPE html>
<html>
<body>
<?php
// Interface definition
interface Animal {
  public function makeSound();
}
// Class definitions
class Cat implements Animal {
  public function makeSound() {
    echo " Meow ";
  }
}
class Dog implements Animal {
  public function makeSound() {
    echo " Bark ";
  }
}
class Mouse implements Animal {
  public function makeSound() {
    echo " Squeak ";
  }
}
// Create a list of animals
$cat = new Cat();
$dog = new Dog();
$mouse = new Mouse();
$animals = array($cat, $dog, $mouse);
// Tell the animals to make a sound
foreach($animals as $animal) {
  $animal->makeSound();
}
?>
 
</body>
</html>

Output :-

Meow Bark Squeak

Example Explained :-

The Animal interface is implemented by Cat, Dog, and Mouse, therefore they can all generate sounds with the makeSound() method. As a result, even if we don't know what kind of animal each one is, we can loop through all of them and order them to create a sound.

A method's implementation is not specified in the interface, which means that each animal's sound is unique.



You can also search for these topics, Using php Interfaces, php implement multiple interfaces, type of php interfaces, how to get php interfaces, php why use interfaces, Example for php interfaces, return type of interfaces in php.