Interface in PHP | PHP OOPs Interfaces

Introduction

PHP is an object-oriented language, and an interface is one of the primary features of an object-oriented programming language. The interface enables the coders to declare the functions containing the different definitions in the class implementing that interface. Read on to understand the interface in PHP. Also, go through the interface examples in PHP shared in the article to get more understanding.

Interface in PHP

An interface is a feature in object-oriented programming that allows creating functions without implementation. The implementation needs to be included in the class. It helps prevent the complexity of the method definition, as each class inheriting the interface can have a different implementation as per the need. The usage of the interface in PHP is similar to the class, with the only difference that implementation is not present in the functions in the interface, and the interface has no variables.

Features of an Interface in PHP

  • The interface does not contain the code.
  • The interface has the declaration of the method with or without arguments but not the definition.
  • A class that is implementing the interface should contain the definition of all the methods declared in the interface.
  • A class can have the implementation of multiple interfaces.
  • An interface cannot have non-abstract methods.

Syntax of an Interface

The interface needs to be declared before use. The syntax of an interface is similar to that of a class, with the only difference being that the interface keyword is used in the place of the class keyword while declaring the interface. Below is the syntax for declaring an interface in PHP:

<?php

           //Declaration of the interface in PHP

           Interface <Interface Name>

           {

                       //Code

           }

?>

The methods declared inside the interface can be accessed by inheriting the interface by class. The class inherits the interface in PHP using the implements keyword while declaring the class.

Below is the syntax to implement an interface in PHP:

<?php

           //Declaration of class

           Class <Class Name> implements <Interface Name>

           {

                       //Code

           }

?>

Interface Examples in PHP

To understand the usage of the interface, let us have a look at the below example:

<?php

              //Declaring interface

              Interface Example_Shape

              {

                             Public function ShapeArea();

              }

              //Class inheriting the interface

              Class Shape1 implements Example_Shape

              {

                             Public function ShapeArea()

                             {

                                            Echo ‘Area of the circle’;

                             }

              }

              //Class inheriting the interface

              Class Shape2 implements Example_Shape

              {

                             Public function ShapeArea()

                             {

                                            Echo ‘Area of the square’;

                             }

              }

              // Class without using the interface

              Class Shape3

              {

                             Public function AreaCal()

                             {

                                            Echo ‘No interface’;

                             }

              }

              $Obj1 = new Shape1; // Object for class1

              $Obj2 = new Shape2; // Object for class2

              $obj3 = new Shape3; // Object for class 3, not using interface

              Echo $obj1->ShapeArea();

              Echo ‘<br>’;

              Echo $obj2->ShapeArea();

              Echo ‘<br>’;

              Echo $obj3->AreaCal();

?>

Output

Area of circle

Area of square

No interface

Explanation of the output

The Shape1 class has the definition of a function declared inside the interface. Shape1 implements the interface, therefore, it has access to the function inside it and prints ‘Area of the circle’. Similar is the explanation for class Shape2 and output ‘Area of the square.’ The class Shape3 is not implementing the interface and works as a usual class in PHP.

Implementation of Multiple Interfaces

A class can implement more than one interface too. In such cases, the class needs to have the definition of all the methods declared in all interfaces that the class is implementing. Below is the illustration of the implementation of more than one interface in PHP.

<?php

           // Declaration of Interface

           Interface example1

           {

                       Public function method1();

           }

           // Declaration of another interface

           Interface example2

           {

                       Public function method2();

           }

           //Class implementing the two interfaces declared above

           class class1 implements example1, example2

           {

                       // Definition of method of interface1

                       Public function method1

                       {

                                   Echo ‘Inside interface1 method1’;

                       }

                       //Definition of the method of interface2

                      Public function method2

                       {

                                   Echo ‘Inside interface2 method2;

                       }

           }

           Obj1 = new class1;

           Obj2 = new class2;

           Obj1->method1;

           Obj2->method2;

?>

Output

Inside interface1 method1

Inside interface2 method2

Few Points to Note About the Implementation of Multiple Interfaces

  • All the methods declared in the interface should be public. 
  • The function cannot have the keyword abstract.
  • If the programmer forgets to define any interface function, the code will throw an error.
  • Interfaces cannot contain variables.

Checkout: OOPs Concepts in PHP

Difference Between an Interface and an Abstract Class

Below are some of the differences between an interface and an abstract class:

  • An interface contains only the declaration, but an abstract class contains both the method declaration and its definition.
  • The methods in the interface can only be public, while the methods in the abstract class can be declared as public, private, or protected.
  • One class can implement more than one interface, while one class can extend one abstract method only.

Conclusion

An interface in PHP is a feature that enables the users to declare public methods without definition. This article explained all the information about the interface with its syntax and examples. We hope the information shared in this article is helpful to you in understanding the concept. You can learn similar topics based on PHP on upGrad blogs. For deep understanding, you can enroll for an online certification course on PHP at upGrad. upGrad offers many certificate courses that can help you with your career and learning.

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE

Leave a comment

Your email address will not be published.

×