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.
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:
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:
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.
- CSS | User Interface
- What is the difference between interface and type in TypeScript ?
- HTML | AbsoluteOrientationSensor Interface
- Principles and Patterns of User Interface Design
- How to check interface type in TypeScript ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | imagecreatetruecolor() Function
- Maximum execution time taken by a PHP Script
- PHP program to fetch data from localhost server database using XAMPP
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | Different characters in the given string
- Difference and Similarities between PHP and C
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.