Interfaces in PHP
Tags: php, interfaces, oop, objectorientedprogramming
Published: 2020-02-09 17:37:52

It's great to work with a team, but when you collaborate team - you'd need some rules to follow Or else everyone would follow their own rules making each day even harder to live. 



Let's say John and Tom wanted to collaborate on a project that calculates different attributes of shapes. John creates a Triangle class and works on it while Tom works on a Rectangle class, they both need a method to calculate the area, John creates a method named 'calculateArea()' while Tom creates a method named getArea(). Now when users need to echo/print out the area of each shape they will need to execute different functions and when eventually John and Tom leave the project, the new developer will have no idea where to start..he  looks at the code - executes it - gets a bunch of errors - gets so frustrated, leaves the job and starts flipping burgers the next day.  

To avoid 'Mess-ups', to make dev team's life easier and avoid confusion we use Interfaces. 

How to implement Interfaces?

To see interfaces in action, create the following folder structure;

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1l4e6cknxln9m31zbx2o.jpg)

And added the following on to shapeInterface.php and shapes.php

shapeInterface.php

```
<?php

interface shapeInterface{
    public function getArea();
}
?>
```

shapes.php

```
<?php

include "shapeinterface.php";

class Triangle implements shapeInterface{

    public function getArea(){
        echo "Triangle Area";
    }
}



class Rectangle implements shapeInterface {
    public function getArea(){
        echo "Rectangle Area";
    }
}

?>

```

And we use them on index.php as shown below;


```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <?php
     include "shapes.php";
      

     $tri =  new Triangle();
     echo $tri->getArea();
     $rec = new Rectangle();
     echo "<br>";
     echo $rec->getArea();
    ?>
</head>
<body>
    
</body>
</html>
```

Next Question, what if we need more than one rule (ie. more than one interface) can we use multiple interfaces ? YES!

How to use multiple interfaces.. 

To the same example above lets add an interface that has some methods like drawContent, drawGraphics etc .. that interface we can call it "Drawable"

Create a file drawableInterface.php

```
<?php

interface drawableInterface{
    public function drawContent();
    
}


?>

```

Now include 'drawableInterface.php' in the shapes.php file and then add an interface  after a comma as shown below.
 
```
<?php

include "shapeinterface.php";
include "drawableInterface.php";

class Triangle implements shapeInterface, drawableInterface{

    public function getArea(){
        echo "Triangle Area";
    }
    public function drawContent(){
        echo "Content Triangle";
    }
}



class Rectangle implements shapeInterface {
    public function getArea(){
        echo "Rectangle Area";
    }
    public function drawContent(){
        echo "Content Triangle";
    }
}

?>
```

Cool, now we have two set of rules.  In other words two interfaces 1) shapeInterface and 2) drawableInterface.  

So please use interfaces in your next project :)