DEV Community

Naveen Dinushka
Naveen Dinushka

Posted on • Edited on

Interfaces in PHP

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

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

shapeInterface.php

<?php

interface shapeInterface{
    public function getArea();
}
?>
Enter fullscreen mode Exit fullscreen mode

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";
    }
}

?>

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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();

}


?>

Enter fullscreen mode Exit fullscreen mode

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";
    }
}

?>
Enter fullscreen mode Exit fullscreen mode

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 :)

Top comments (2)

Collapse
 
hackergaucho profile image
Hacker Gaucho • Edited

good article. interfaces are cool when using an IDE like PHPStorm that automatically recognizes them. from what I remember I never used interfaces in a separate file like you did in this example.

since I switched from PHPStorm to Atom I stopped using interfaces because I was too lazy to write them but I recognize that they are very important and necessary.

your article left me with two doubts:

  1. is there a PSR standard for naming the interfaces?
  2. is there any advantage in placing the interface in a separate file instead of in the same file as your class?
Collapse
 
naveenkolambage profile image
Naveen Dinushka

is there a PSR standard for naming interfaces? This is one php-fig.org/bylaws/psr-naming-conv...

is there any advantage in placing the interface in a separate file instead of in the same file as your class? stackoverflow.com/questions/171563...