PHP Interface Inheritance and Interface Multiple Inheritance Principle and Implementation Method Detailed Explanation

  • 2021-08-10 07:26:34
  • OfStack

In this paper, the principle and implementation method of PHP interface inheritance and interface multi-inheritance are described by examples. Share it for your reference, as follows:

In the interface of PHP, the interface can inherit the interface. Although the PHP class can only inherit one parent class (single inheritance), interfaces are different from classes, and interfaces can implement multiple inheritance and can inherit one or more interfaces. Of course, the inheritance of interfaces also uses extends keywords. If you want multiple inheritance, you only need to separate the inherited interfaces with commas.

It should be noted that when your interface inherits other interfaces, it directly inherits the static constant properties and abstract methods of the parent interface, so the class must implement all related abstract methods when implementing the interface.

Now that you know something about the inheritance of PHP interface, the following example is for reference, and the code is as follows:


<?php
interface father{
  function shuchu();
}
interface fam extends father{
  function cook($name);
}
class test implements fam{
  function shuchu(){
    echo " Interface inheritance, to implement two abstract methods ";
    echo "<br>";
  }
  function cook($name){
    echo " People who often cook at ordinary times are: ".$name;
  }
}
$t=new test();
$t->shuchu();
$t->cook(" Mom ");
?>

The code runs as follows:


 Interface inheritance, to implement two abstract methods 
 The person who often cooks at ordinary times is: Mom 

The above example is that the interface inherits one interface, so when the test class implements the fam interface, it is necessary to instantiate two abstract methods, that is, to instantiate both the abstract methods of the subclass and the parent class of the interface.

Let's look at an example of multiple inheritance of interfaces. The code is as follows:


<?php
interface father{
  function shuchu();
}
interface mother{
  function dayin($my);
}
interface fam extends father,mother{
  function cook($name);
}
class test implements fam{
  function dayin($my){
    echo " My name is: ".$my;
    echo "<br>";
  }
  function shuchu(){
    echo " Interface inheritance, to implement two abstract methods ";
    echo "<br>";
  }
  function cook($name){
    echo " People who often cook at ordinary times are: ".$name;
  }
}
$t=new test();
$t->shuchu();
$t->dayin(" Xiaoqiang ");
$t->cook(" Mom ");
?>

Sample run results:


 Interface inheritance, to implement two abstract methods 
 My name is: Xiaoqiang 
 The person who often cooks at ordinary times is: Mom 

Because the interface inherits two interfaces, all the abstract methods of these three abstract classes should be instantiated in all instances, with a total of three. After reading these two examples, you should be familiar with the inheritance of interfaces. In fact, there are only one single inheritance and multiple inheritance, as long as all relevant abstract methods are implemented.

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: