What is the point of interfaces in PHP ?
Last Updated :
24 Oct, 2019
An Interface allows users to create programs 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. It resembles the abstract methods, resembling the abstract classes. An Interface is defined just like a class is defined but with the class keyword replaced by the interface keyword and the function prototypes. The interface contains no data variables. The interface is helpful in a way that it ensures to maintain a sort of metadata for all the methods a programmer.
In PHP, we can do Object-Oriented Programming. That means we can make use of Object-Oriented concepts like create classes and objects, use inheritance, etc. OOPs contains mainly three different types of inheritance. Which are: Single Inheritance, Multiple Inheritance, and Multi-level Inheritance. Now, PHP supports single and multi-level inheritance. Multiple inheritance is not directly supported in PHP but can be implemented using Interface. So, the main reason for Interfaces in PHP is to implementing Multiple Inheritance.
In multiple inheritance, we can derive a child class from two or more parent classes. Here is how we can implement multiple inheritance in PHP.
We have three different classes as follows:
class Circle {
public function draw(){
//
}
public function setRadius() {
//
}
}
class Square {
public function draw(){
//
}
}
class Rectangle {
public function draw(){
//
}
}
Now as it is shown above, we have the same function in all the three classes
draw() and one more function in the class Circle as
setRadius(). Now we have three different interfaces created and implemented them in our classes as follows:
interface Shape {
public function draw();
}
interface Radius {
public function setRadius();
}
interface Main {
public function process();
}
class Circle implements Shape, Radius, Main {
public function draw(){
echo "Drawing Circle...";
}
public function setRadius() {
echo "Setting Radius...";
}
public function process() {
$this->setRadius();
$this->draw();
}
}
class Square implements Shape, Main {
public function draw(){
echo "Drawing Square...";
}
public function process() {
$this->draw();
}
}
class Rectangle implements Shape, Main {
public function draw(){
echo "Drawing Rectangle...";
}
public function process() {
$this->draw();
}
}
As it is shown above, we have implemented more that one interface in our classes. The
Shape and
Main interfaces are implemented in all the classes but
Radius interface is just implemented in Circle class.
Then a fourth class called
DrawShape is there as we want to call the
process() function in all the classes. The
process() function is to call all the functions in that class.
class DrawShape {
public function newShape(Main $shape) {
return $shape->process();
}
}
Example:
PHP
<?php
interface Shape {
public function draw();
}
interface Radius {
public function setRadius();
}
interface Main {
public function process();
}
class Circle implements Shape, Radius, Main {
public function draw(){
echo "Drawing Circle...";
echo "<br>";
}
public function setRadius() {
echo "Setting Radius...";
}
// To call all the functions in this class
public function process() {
$this->setRadius();
$this->draw();
}
}
class Square implements Shape, Main {
public function draw(){
echo "Drawing Square...";
echo "<br>";
}
// To call all the functions in this class
public function process() {
$this->draw();
}
}
class Rectangle implements Shape, Main {
public function draw(){
echo "Drawing Rectangle...";
echo "<br>";
}
// To call all the functions in this class
public function process() {
$this->draw();
}
}
class DrawShape {
public function newShape(Main $shape) {
return $shape->process();
}
}
// To Draw Circle
$shapeCircle = new Circle();
$drawCircle = new DrawShape();
$drawCircle->newShape($shapeCircle);
// To Draw Square
$shapeSquare = new Square();
$drawSquare = new DrawShape();
$drawSquare->newShape($shapeSquare);
// To Draw Rectangle
$shapeRectangle = new Rectangle();
$drawSquare = new DrawShape();
$drawSquare->newShape($shapeRectangle);
?>
Output:
Similar Reads
What is the use of â=>â symbol in PHP ? The '=>' symbol is used to assign key value pairs in an array. The value of left side is called key and the value of right side is called value of key. It is mostly used in associative array. Syntax: key => value Example 1: PHP program to create associative array using '=>' symbol. php <?ph
2 min read
What are getters and setters methods in PHP ? In object-oriented programming, getters and setters are methods used to access and modify the private or protected properties of a class. These methods provide a controlled way to retrieve and update the values of class properties, promoting encapsulation and maintaining the integrity of an object's
3 min read
What is the difference between MySQL, MySQLi and PDO? To understand the difference between MySQL, MySQLi, and PDO, we must know about each one of them individually. These are nothing but the APIs of PHP that is used to access the MySQL databases and tables. The developers can choose either one of them for their project, however, it must be known that M
6 min read
What's New in PHP 7 ? Pre-requisite: PHP 7 | Features Set 1 PHP 5 seen many minor releases, being exciting along the way, including the provision of Object-Oriented programming and many features correlated with it. So, why 7 and not 6? Almost all the features being considered for PHP 6 were ultimately executed in PHP 5.3
6 min read
Scope Resolution operator in PHP The scope resolution operator also known as Paamayim Nekudotayim or more commonly known as the double colon is a token that allows access to static, constant, and overridden properties or methods of a class. It is used to refer to blocks or codes in context to classes, objects, etc. An identifier is
2 min read