The Wayback Machine - https://web.archive.org/web/20240607044335/https://www.geeksforgeeks.org/php-interface/
Open In App

PHP | Interface

Last Updated : 03 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

An Interface allows the users to create programs, specifying the public methods that a class must implement, without involving the complexities and details of how the particular methods are implemented. It is generally referred to as the next level of abstraction. It resembles the abstract methods, resembling the abstract classes. An Interface is defined just like a class is defined but with the class keyword replaced by the interface keyword and just the function prototypes. The interface contains no data variables. The interface is helpful in a way that it ensures to maintain a sort of metadata for all the methods a programmer wishes to work on.

Creating an Interface

Following is an example of how to define an interface using the interface keyword.




<?php 
  
interface MyInterfaceName {
   public  function methodA();
   public  function methodB();
} 
  
?>


Few characteristics of an Interface are:

  • An interface consists of methods that have no implementations, which means the interface methods are abstract methods.
  • All the methods in interfaces must have public visibility scope.
  • Interfaces are different from classes as the class can inherit from one class only whereas the class can implement one or more interfaces.

To implement an interface, use the implements operator as follows:




<?php
  
class MyClassName implements MyInterfaceName{
   public  function methodA() { 
  
     // method A implementation
   } 
   public  function methodB(){ 
  
     // method B implementation
   } 
}
  
?>


Concrete Class: The class which implements an interface is called the Concrete Class. It must implement all the methods defined in an interface. Interfaces of the same name can’t be implemented because of ambiguity error. Just like any class, an interface can be extended using the extends operator as follows:




<?php 
  
interface MyInterfaceName1{
  
    public  function methodA();
   
}
  
interface MyInterfaceName2 extends MyInterfaceName1{
  
    public  function methodB();
}
  
?>


Example:




<?php 
  
interface MyInterfaceName{
  
    public function method1();
    public function method2();
  
}
  
class MyClassName implements MyInterfaceName{
  
    public function method1(){
        echo "Method1 Called" . "\n";
    }
  
    public function method2(){
        echo "Method2 Called". "\n";
    }
} 
  
$obj = new MyClassName;
$obj->method1();
$obj->method2();
  
?>


Output:

Method1 Called
Method2 Called

Advantages of PHP Interface

  • An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
  • An interface can model multiple inheritances because a class can implement more than one interface whereas it can extend only one class.
  • The implementation of an inheritance will save the caller from full implementation of object methods and focus on just he objects interface, therefore, the caller interface remains unaffected.


Previous Article
Next Article

Similar Reads

CSS | User Interface
User Interface (UI) defines the way humans interact with the information systems. In Layman’s term, User Interface (UI) is a series of pages, screens, buttons, forms and other visual elements that are used to interact with the device. Every app and every website has a user interface. The user interface property is used to change any element into on
3 min read
JavaScript | ResizeObserver Interface
The ResizeObserver Interface is used to provide a mechanism to monitor the changes to the dimensions of an element. The notifications of the changes are delivered to the observer whenever changes take place. This is a useful API as traditional methods of monitoring the changes were composed of hacks or were poor in performance. The traditional meth
3 min read
Unity | Introduction to Interface
The article "Game Development with Unity | Introduction" introduces about Unity and how to install it. In this article, we will see how to create a new project and understand the interface of the Unity Game Editor. Creating a new project Open the Unity Hub. Click on New button at the top right. Select 3D Project. Give a Project Name and create the
3 min read
How to Choose the Best Colors For The User Interface?
Coca-Cola, McDonald’s, Dell, Cafe Coffee day, Paytm, Starbucks..... What comes to your mind when you think about these famous services or brands... Maybe your favorite delicious food, gadgets, drinks, or maybe the iconic logo and color of these brands which they have used to make their products and services more attractive. Now read some interestin
8 min read
HTML | AbsoluteOrientationSensor Interface
Absolute Orientation Sensor: The AbsoluteOrientationSensor is a fusion sensor that measures the rotation of a device in relation to the Earth’s coordinate system. The rotation of devices on world coordinates is based on real accelerometer, gyroscope, and magnetometer sensors. AbsoluteOrientationSensor Constructor: To describe the device's physical
2 min read
Principles and Patterns of User Interface Design
User Interface Design is a platform where the effective interaction of the user with the system can be seen. A user more often judges a system by its interface rather than its functionality. An interface that is designed poorly or not leads to improper use of the system by the user. Many users in business systems interact with the systems using a g
3 min read
How to check interface type in TypeScript ?
Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an interface or contract of a car. interface Audi { len
2 min read
How to pass Node.js Output to Web Interface ?
Most of the popular browsers today automatically handle the display of raw data output from API on the Web interface. To do this we just have to send the required data directly in API response and the browser will take care of the rest. Modules Required: express.js: To handle routing Setting up environment and Execution: Step 1: Initialize node pro
1 min read
Generics Interface in typescript
"A major part of software engineering is building components that not only have well-defined and consistent APIs but are also reusable. " This sentence is in the official documentation we would start with. There are languages that are strong in static typing &amp; others that are weak in dynamic typing. At this point, Typescript has shown to have w
5 min read
How to Create Windows 11 Interface using HTML5 ?
In this article, we will see how to create a Windows 11 interface using HTML5. Approach: To create Windows 11 interface we will use HTML, CSS, and JavaScript. If you want to change to design you can add more functionality to it. In this article, we will divide the whole thing into three different parts creating the structure, styling the structure,
4 min read
Article Tags :