PHP OOPs Interface - PHP Interface



PHP OOPs Interface

  • PHP interfaces allow a programmer to create code which specifies which methods a class must implement, without having to define how these methods are handled.
  • Interfaces are defined using the interface keyword and are defined in the same way as a standard class, but without any of the methods having their contents defined.
  • All methods declared in an interface must be public, this is the nature of an interface.
php-interface

Syntax

<?php
interface InterfaceName {
  public function someMethod1();
  public function someMethod2($name, $color);
  public function someMethod3() : string;
}
?>

Sample Code

<?php
interface Pet {
  public function barks();
}
class Dog implements Pet {
  public function barks() {
    echo "Bow Bow";
  }
}
$pet = new Dog();
$pet->barks();
?>

Output

Bow Bow

Related Searches to PHP OOPs Interface - PHP Interface