PHP and MySQL (56 Blogs) Become a Certified Professional

How To Implement Interface In PHP?

Published on Aug 19,2019 1.1K Views

How To Implement Interface In PHP?

Interface In PHP is generally needed to have the same type of nomenclature in the code system. Suppose you are working with a team and you have created some function and other team members also have created the same type of function with another name. Obviously, there will be a conflict. So let us discuss this concept in detail.

Following pointers will be covered in this article,

So let us get started then,

Interface In PHP

It allows the users to create programs by specifying the public methods that a class must implement, without involving the complexities and details of how the particular methods are implemented. It is generally referred to as the next level of abstraction and commits its child classes to abstract methods that they should implement.

Below code demonstrates the example of an interface.

Example for Interface

<?php class Circle { public function getArea() { echo 'Circle Area'; } } class Square { public function CalculateArea() { echo 'Square Area'; } } $circ=new Circle; echo $circ->getArea();
?>

This above code results with the area of the circle. Obviously, if the user wants to get the area of a square, he will surely do the same thing for square also by calling getArea(). This will give a conflict because square does not have any function called getArea(). So you have force your team or yourself to create the same functionality or the same nomenclature for the whole of your project.

For this, I can create an interface. Interface just forces you to have some functions in your class.

Moving on with this Interface in PHP article

How Interface Solves Problem?

ShapeInterface.php:

<?php interface shapeInterface { //You don't need to give the structure of getArea public function getArea(); } ?>

This interface will tell the classes which are going to implement the interface that you have to implement this function otherwise I will give you an error.


<?php
include "ShapeInterface.php";
class Circle implements ShapeInterface
{
public function getArea()
{
echo 'Circle Area';
}
}
class Square implements ShapeInterface
{
public function getArea()
{
echo 'Square Area';
}
}
$circ=new Circle;
$squ=new Square;
echo $circ->getArea();
echo "<br>";
echo $squ->getArea();
?>

If you are not giving the function decided by the interface then you will get an error. You can even get more than one interface for a single class. You just have to give the name of the interface by using a comma.

geometryInterface.php

<?php
interface geometryInterface
{
public function getCircumference($value1, $value2);
}
?>
<?php
include "ShapeInterface.php";
include "geometryInterface";
class Circle implements ShapeInterface,geometryInterface
{
public function getArea()
{
echo 'Circle Area';
}
public function getCircumference($value1, $value2)
{
echo $value1 . "<br>" . $value2. "<br>";
}
}
class Square implements ShapeInterface
{
public function getArea()
{
echo 'Square Area';
}
}
$circ=new Circle;
$squ=new Square;
echo $circ->getArea();
echo "<br>";
echo $circ->getCircumference(21,23);
echo $squ->getArea();
?>

With this we come to an end of this article, I hope you understood interface in php by going through various scenarios.

If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Interface in PHP” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Interface In PHP?

edureka.co