Home  >  Article  >  Backend Development  >  PHP Interface

PHP Interface

WBOY
WBOYOriginal
2024-08-29 13:00:281162browse

PHP Interface helps us make useful programs, indicating the class’s public methods that must be executed without even including the complexities and how our specific methods will be implemented. PHP Interface will only define arguments and names rather than the contents of the methods, and any class which implements the interface needs to implement all the methods defined by the interface.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

These interfaces are similar to the class, but only the class phrase will be replaced by the interface keyword in the declaration.

Syntax:

<?php
Interface interface_name
{
//List of methods
}
?>

Why Do We Need PHP Interface?

It can implement one interface in the same class but make more interfaces within the same class. Classes can implement specific methods with PHP Object interfaces without being restricted in how to implement them. The interface is the same or similar to the class, but the interface keyword of PHP replaces the class keyword without using any methods with their contents defined. You can declare a constructor within an interface. Class or Classes can implement multiple interfaces when needed by separating each and every interface with a comma.

You can extend classes in PHP Programming Language using the “extends” operation. PHP Interfaces are the next level of abstraction. It is similar to the abstract classes, but there is a slight difference. Interfaces or Interfaces allow you to create the code, which is helpful to define the methods of the classes, but you can’t even add any code to those methods, whereas the abstract classes allow the same or similar thing as the PHP interface or interfaces but in abstract classes, one can add the code to the method or methods.

Working on PHP Interface

Older versions before PHP 5.3.9 couldn’t even implement just two interfaces with the same name because it would cause some ambiguity. Most of all, the recent PHP versions allow duplicate methods with the same/similar signature. The class that implements the interface should use the method signatures compatible with the “Liskov Substitution Principle – LSP.” There will be a fatal error in the result if the method signatures are not used.

The class will use the interface to add content to its methods, consisting solely of methods without any pre-existing content. Methods in the interfaces are mostly visible to the public with no restrictions. PHP interfaces are not the same/similar to the classes. Classes can inherit many/multiple interfaces, but class can inherit only one class at a time. It has this advantage too. Variables are present inside the interface/interfaces.

Examples of PHP Interface

Below are examples of PHP Interfaces:

Example #1

In the example below, we declared an interface named “MyInterfaceName1” with only two methods mentioned. They are method11 and method21 inside of the interface without using any content. Then a class named “MyClassName1” will implement the interface “MyInterfaceName1” and use the available methods based on the requirement.

As the definition said, the interface also consists of methods without content. During the implementation of the interface, we will discuss the content only in class. Sometimes, people refer to these interface methods as abstract methods.

Code:

<?php
interface MyInterfaceName1{
public function method11();
public function method21();
}
class MyClassName1 implements MyInterfaceName1{
public function method11(){
echo "Now Method11 Called" . "<br>";
}
public function method21(){
echo "Now Method21 Called". "\n";
}
}
$obj = new MyClassName1;
$obj->method11();
$obj->method21();
?>

Output:

PHP Interface

Example #2

The program below contains two interfaces, each with its own method. Then a class bird is made with a method called info(). Now the class Dove extends to the Class Bird, which implements the CanFly PHP Interface to get the print statements because print statements/other will not be present inside the interfaces because it will have only methods.

Based on the characteristics of the Bird 2 interfaces are made, “CanFly” and “CanSwim” with 1 method in each. They are fly() and swim(). The method “info()” in the class “Bird” prints/echoes statements to determine the bird’s type and if it is indeed a bird. Extend the “bird” class and implement the “CanFly” and “CanSwim” interfaces to place the details of birds like Dove, Penguin, and Duck one by one in the “$name1” variable. Also, add echo statements for the interfaces.

We have created a function named “describe($bird)” that utilizes the “CanFly” and “CanSwim” interfaces to determine if a creature is a bird. If the $ bird doesn’t satisfy the fly() and swim(), the else condition exists in the PHP program’s output. Now calling all the described functions with the 3 bird classes. You will get the output of the 3 birds as needed. Check out below. It is a simple description, but you will know how it is happening if you look at the code and output of the PHP Program below.

Code:

<?php
/**
* This is An example of the duck typing in the PHP Script
*/
interface CanFly1 {
public function fly1();
}
interface CanSwim1 {
public function swim1();
}
class Bird1 {
public function info1() {
echo "I am a {$this->name1}\n";
echo "I am a bird\n";
}
}
/**
* This is some of the implementations of the birds
*/
class Dove1 extends Bird1 implements CanFly1 {
var $name1 = "Dove";
public function fly1() {
echo "I fly\n";
}
}
class Penguin1 extends Bird1 implements CanSwim1 {
var $name1 = "Penguin";
public function swim1() {
echo "I swim\n";
}
}
class Duck1 extends Bird1 implements CanFly1, CanSwim1 {
var $name1 = "Duck";
public function fly1() {
echo "I fly\n";
}
public function swim1() {
echo "I swim\n";
}
}
/**
* This is one of the simple function which is to describe a bird
*/
function describe1($bird1) {
if ($bird1 instanceof Bird1) {
$bird1->info1();
if ($bird1 instanceof CanFly1) {
$bird1->fly1();
}
if ($bird1 instanceof CanSwim1) {
$bird1->swim1();
}
} else {
die("This is not a bird. I cannot describe it.");
}
}
// Now describing the birds
describe1(new Penguin1);
echo "---\n<br>";
describe1(new Dove1);
echo "---\n<br>";
describe1(new Duck1);

Output:

PHP Interface

Advantages

Following are some of the advantages given.

  • It will allow the unrelated classes to implement similar methods without considering their positions in the class with an inheritance hierarchy.
  • We can model many inheritances in this approach by implementing multiple interfaces in a class, but the class can only extend one class.
  • It can implement an inheritance which can save the caller from the implementation of the object methods fully. This implementation of the heritance concept is also helpful to focus on the object’s interface so that the caller interface doesn’t even affect it.

Recommended Article

This is a guide to the PHP Interface. Here we discuss the PHP Interface, its Advantages, and its various examples to understand the PHP Interface concept briefly. You can also go through our other suggested articles to learn more –

  1. PHP Encryption
  2. PHP Frameworks
  3. Abstract Class in PHP
  4. PHP Superglobal Variables

The above is the detailed content of PHP Interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP ziparchiveNext article:PHP ziparchive