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.
What is an interface in Java?
A Java interface is used as a blueprint of a class. It consists of abstract methods and classes. An interface in java can only have abstract methods and doesn’t require a method body. Moreover, through a Java interface, abstraction and multiple inheritances are achievable. To further simplify, interfaces only contain variables and methods and don’t have a method body. The only way to declare an interface is by using the interface keyword. By default, all the fields are public, static, and final. Multiple inheritances in Java aren’t supported by Java classes due to their ambiguity. There is an implementation class that takes care of interface implementation.
How is PHP and Object-Oriented Language and what is OOP?
An object-oriented programming language is a methodology that will have object-oriented functionalities like encapsulation, abstraction, data binding, polymorphism, and inheritance. Earlier, PHP used C++ language and was based on this language only. C++ is also an object-oriented programming language that automatically makes PHP an OOP. A PHP class creates objects of any class through methods and properties. Each object in a class will have its own values and properties.
What are encapsulation, coupling, and polymorphism in OOPs concepts?
Polymorphism is the technique to perform one task in multiple ways. To achieve polymorphism, method overriding and method overloading are generally used. Coupling is usually the dependency of a class through its knowledge. Coupling happens when the class knows about each other. If one class contains details of another class, that is coupling. There are several methods that are used to protect the identity of the classes. Encapsulation is wrapping up the data along with the code in a single unit. It is similar to a capsule that has medicines inside it. Classes are examples of encapsulation. An example of a fully encapsulated class is Java Bean, where all its data members are private.