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

Select Coursecaret down icon
Selectcaret 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

Best Diploma Courses To Do List in 2024
60222
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

19 Feb 2024

Top 15 Highest Paid IT Jobs in India for Freshers &#038; Experienced [A Complete Guide]
922689
Summary: In this Article, you will learn top 15 highest paid IT jobs in India for freshers & Experienced. Top 15 Highest Paid IT Jobs Average
Read More

by upGrad

19 Feb 2024

21 Best Computer Programming Courses To Get a Job in 2024
909101
Summary: In this Article, you will learn 23 Best Computer Programming Courses To Get a Job 1. Python 2. R and Software Development 3. Fundamentals of
Read More

by Kamal Jacob

19 Feb 2024

10 Best Job-Oriented Short Term Courses Which are In-Demand [updated 2024]
812099
Summary: In this article, you will learn the 10 Best Job-Oriented Short-Term Courses which are In-Demand in 2024. Take a glimpse below. Product Mana
Read More

by Kamal Jacob

19 Feb 2024

15 Top Career Options After Engineering: Trending in 2024
360736
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

18 Feb 2024

12 Best Career Options after M.Com: What to do After M.Com? [2024]
279213
Summary In this article, you will learn about the 12 Best Career Options after M.Com. Take a glimpse below. Enrol in an MBA Program Become a Chartere
Read More

by Rahul Karthikeyan

18 Feb 2024

Top 6 Highest Paying Jobs in Commerce Without Maths [For Freshers &#038; Experienced]
904185
This article will tell you about the highest paying jobs in commerce without maths. Life is unimaginable without maths. But several careers pay decent
Read More

by Sriram

18 Feb 2024

16 Top Career Options After Computer Engineering [Trending in 2024]
282933
Summary: In this Article, you will learn about 15 top career options after diploma in computer engineering: Big Data Engineer Machine Learning Engin
Read More

by Sriram

18 Feb 2024

Top 10 Best Career Options for Science Students: Which Should You Select in 2024
201826
Choosing a career stream or science line job is one of the most path-breaking moments of an individual’s life. It defines the future course of their p
Read More

by Dilip Guru

18 Feb 2024

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