Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconGeneralbreadcumb forward arrow iconInterface in PHP | PHP OOPs Interfaces

Interface in PHP | PHP OOPs Interfaces

Last updated:
3rd Mar, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
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

Ads of upGrad blog

           //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.
Ads of upGrad blog

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation

Selectcaret down icon
Select Area of interestcaret down icon
Select Work Experiencecaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular MBA Course

Frequently Asked Questions (FAQs)

1What 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.

2How 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.

3What 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.

Explore Free Courses

Suggested Blogs

23 Best Computer Programming Courses To Get a Job in 2024
915445
Summary Best computer programming courses are always in demand. Especially now. Let’s learn about the list of top computer courses available now that
Read More

by Kamal Jacob

16 Jul 2024

Why Should You Be Hired For This Internship Interview Question &#8211; 5 Winning Ways to Answer
514125
Ever wondered why employers stress internships? It’s because they understand the immense value that internships bring to the table. As someone w
Read More

by Nitin Gurmukhani

13 Jul 2024

Best Diploma Courses To Do List in 2024
63567
Merely graduation does not qualify in today’s competitive market. Hence, having post-graduate diploma courses give you better weightage and know
Read More

by Dilip Guru

13 Jul 2024

15 Top Career Options After Engineering: Trending in 2024
371832
Summary: In this article, you will learn about 15 top career options after Engineering. Higher Studies Public Service Undertakings Management Entrep
Read More

by Nitin Gurmukhani

01 Jul 2024

Top 25 Highest Paying Jobs in the World in 2024 [A Complete Guide]
1141510
Summary In this article, you will learn about the top 25 highest-paying jobs in the world. And you will be able to get the answer to ‘Which job has t
Read More

by Nitin Gurmukhani

27 Jun 2024

Future Scope of Marketing: Scope, Salary, Career Opportunities
27027
On an everyday basis, we are introduced to at least one new product, service, or brand. It doesn’t matter in which form you are introduced to them- hu
Read More

by Kamal Jacob

25 Jun 2024

Best Career Options in Sociology: 9 Top Career Opportunities in India
87125
One of the most significant yet scarcely explored careers, sociology owns enormous potential with bright career opportunities for students who are int
Read More

by Nitin Gurmukhani

20 Jun 2024

Top 25 Highest Paying Jobs in India [2024] &#8211; Latest &#038; Trending
2239430
Summary: Professions / Jobs Average Salary Per Annum 2024 Highest Salary Per Annum 2024 Medical Professionals ₹10 Lakhs ₹20 Lakhs Data
Read More

by Nitin Gurmukhani

05 Jun 2024

Top 19 Online Certificate Courses With High Salary in India [2024]
936348
If you’re someone who’s pursued the conventional education path all your life – physical classrooms and face-to-face learning – it is natu
Read More

by Rohan Vats

03 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon