Interface in PHP

In this tutorial, we will learn about Interface in PHP. The interface gives the ability to programmer to define what public methods a class should implement. An interface can contain only abstract methods it means we only declare methods names in the interface and after that, we define the body of these methods in a class that implements that particular interface. The interface contains no data variables. Multiple classes can implement to the same interface and this is known as polymorphism. The main idea to use interface in object-oriented programming is to force the class to override methods that are declared in the interface.

Note! we define a class using class keyword. But to define an interface we use interface keyword in php

Here is an example program that will help you to understand the basic syntax of an interface.

Example Program:

<?php
interface StudentInterface {
  public function setData($name, $age);
  public function showData();
}

Characteristics of Interface:

  • The interface only contains abstract methods.
  • All the methods of an interface must have public visibility scope.
  • A class can inherit from one class but a class can inherit multiple interfaces.
  • You do not need to write abstract keyword manually with methods in the interface. 
  • The interface can not contain member variables(properties).

Till now we have learned how we can define an interface in PHP. But here a question arises that how to use the interface after defining it.

Using Interface in PHP:

<?php
interface StudentInterface {
  public function setData($name, $age);
  public function showData();
}

class Student implements StudentInterface{
    private $name, $age;
     
    function setData($name, $age){
       $this->name=$name;
       $this->age=$age;
    }


    function showData(){
      echo "Hello $this->name your age is $this->age";
    }
}

$stu1 =new Student;
$stu1->setData("Ravi",22);
$stu1->showData();
?>

Note! to inherit a class in another class we use extends keyword but to implement and interface in class we use implements keyword.

What if we do not override methods of the interface in a class: 

<?php
interface StudentInterface {
  public function setData($name, $age);
  public function showData();
}

class Student implements StudentInterface{
    function sayHello(){
      echo "Hello user";
    }
}

$stu1 =new Student;
$stu1->sayHello();
?>

As in the above program, you can see methods of the StudentInterface interface are not overridden in Student class. That is why this program will not run but it will throw the following error message.

PHP Fatal error: Class Student contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (StudentInterface::setData, StudentInterface::showData)

Difference between Abstract class and Interface:

Abstract class Interface
An abstract class can have both abstract and non-abstract methods. But an Interface can have only abstract methods.
An Abstract class can have both member variables. But Interface can not have member variables.
An abstract class can have class methods like public, protected, etc. But in the case of interface, all methods must be public.
We can implement an interface in an abstract class. But in an Interface can’t implement an abstract class.
The abstract keyword is used to declare an abstract class. The interface keyword is used to declare an interface.
To inherit the abstract class in our class we use the “extends” keyword. To inherit an interface in our class we use the “implements” keyword.
Spread the love
Scroll to Top
×