How to implement interface inheritance and interface multiple inheritance principles in PHP

黄舟
Release: 2023-03-16 17:36:01
Original
2104 people have browsed it

This article mainly introduces the principles and implementation methods of PHP interface inheritance and interface multi-inheritance. It briefly describes the concepts and principles of interface inheritance and multi-interface inheritance, and gives the specific implementation and use of PHP interface inheritance in the form of examples. For operational skills, friends in need can refer to

. The examples in this article describe the principles and implementation methods of PHP interface inheritance and interface multi-inheritance. Share it with everyone for your reference, the details are as follows:

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

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

Now you have some understanding of the inheritance of PHP interfaces. The following example is for reference. The code is as follows:


<?php
interface father{
  function shuchu();
}
interface fam extends father{
  function cook($name);
}
class test implements fam{
  function shuchu(){
    echo "接口继承,要实现两个抽象方法";
    echo "<br>";
  }
  function cook($name){
    echo "平时经常做饭的人是:".$name;
  }
}
$t=new test();
$t->shuchu();
$t->cook("妈妈");
?>
Copy after login

The code running results are as follows:


接口继承,要实现两个抽象方法
平时经常做饭的人是:妈妈
Copy after login

The above example is that the interface inherits an interface, so when the test class implements the fam interface, it needs to instantiate two abstract methods, that is, the subclass of the interface and the parent class The abstract methods of a class are all instances.

Let’s look at an example of interface multiple inheritance. 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;
    echo "<br>";
  }
  function shuchu(){
    echo "接口继承,要实现两个抽象方法";
    echo "<br>";
  }
  function cook($name){
    echo "平时经常做饭的人是:".$name;
  }
}
$t=new test();
$t->shuchu();
$t->dayin("小强");
$t->cook("妈妈");
?>
Copy after login

Example running results:


接口继承,要实现两个抽象方法
我的名字是:小强
平时经常做饭的人是:妈妈
Copy after login

Because the interface inherits two interfaces, this code needs to instantiate all the abstract methods of these three abstract classes for all instances. There are three in total. After reading these two examples, you should be familiar with interface inheritance. In fact, there is single inheritance and multiple inheritance, as long as all relevant abstract methods are implemented.

The above is the detailed content of How to implement interface inheritance and interface multiple inheritance principles in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!