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

Top 15 Highest Paying Non-IT Jobs in India [2024]
926321
Summary: In this Article, you will learn about top 15 highest paying Non-IT jobs in India. Highest paying Non-IT Jobs Salary per Annum Business
Read More

by Dilip Guru

16 Apr 2024

Top 10 Best Career Options for Science Students: Which Should You Select in 2024
203752
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

16 Apr 2024

Top Career Options After 12th Science: What Course To Do After 12th Science
354845
Summary In this article, you will learn the Top Career Options After 12th Science. Take a glimpse at the below fields Medicine Engineering Business
Read More

by Kamal Jacob

15 Apr 2024

Top 15 Trending Online Courses in 2024 [For Both Students &#038; Working Professionals]
166836
Professional certifications are invaluable tools for enhancing knowledge and skills in today’s competitive job market. They showcase your dedica
Read More

by Nitin Gurmukhani

15 Apr 2024

10 Best Job-Oriented Short Term Courses Which are In-Demand [updated 2024]
835427
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

15 Apr 2024

Top 25 Highest Paying Jobs in the World in 2024 [A Complete Guide]
1124463
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

14 Apr 2024

Best Career Options after 12th PCM: Top Courses, Salary Expectation
19451
After completing 12th Science PCM, the array of career options is vast. However, it’s common for students to feel overwhelmed by the choices ava
Read More

by Nitin Gurmukhani

14 Apr 2024

6 Top Career Options after BBA: What to do After BBA? [2024]
359230
Summary: In this Article, you will learn 6 Top Career Options after BBA in 2023 Specialize in Management (MBA) Become a Data Scientist Join Public S
Read More

by Kamal Jacob

14 Apr 2024

Top 5 Highest Paying Freelancing Jobs in India [For Freshers &#038; Experienced]
917916
“How well do freelance jobs pay?” Though the freelance economy is still emerging and developing, this particular question is always lurkin
Read More

by Nitin Gurmukhani

14 Apr 2024

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