How to use Interface in php with example 2021
How to use Interface in php with example 2021

How to use Interface in php with example

Interface in PHP: The interface is an important element in PHP which is called reference type. It is the blueprint of the class. It is a collection of abstract methods/functions. It is declared with the interface keyword. 

PHP Language

We can not create an instance(object) of an interface. We can not define a function inside the interface only can be declared, so it is the responsibility of the derived class to implement the method/function of the interface. 

Python Language

How to use Interface in php with example

<?php

interface Geometry

{

 public function rec_area();

 public function tri_area();

}

class Derived implements Geometry

{

 public function rec_area() 

  {

  $h=20;

  $w=20;

  echo "Area of Rectangle:".($h*$w)."<br>";

  }

  public function tri_area() 

  {

  $b=25;

  $h=28;

  echo "Area of Triangle:".(0.5*$b*$h)."<br>";

  }

}

$obj = new Derived();

$obj->rec_area();

$obj->tri_area();

?>

*****OUTPUT*****

Area of Rectangle:600

Area of Triangle:350