Multiple Inheritance of PHP Interface and Method of Realizing Multiple Inheritance Effect of tarits

  • 2021-09-04 23:43:08
  • OfStack

This paper describes the method of PHP interface multi-inheritance and tarits multi-inheritance effect. Share it for your reference, as follows:

Interface multiple inheritance

In the object-oriented of PHP, interfaces can inherit interfaces. The PHP class can only inherit one parent class (single inheritance), but interfaces can implement multiple inheritance and can inherit one or more interfaces. Of course, the inheritance of the interface is also the same as the inheritance of the class 1 using extends keyword, 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.

Here's an example:

1. Inherit a single interface


<?php
interface testA{
  function echostr();
}
interface testB extends testA{
  function dancing($name);
}
class testC implements testB{
  function echostr(){
    echo " Interface inheritance, to implement all related abstract methods! ";
    echo "<br>";
  }
  function dancing($name){
    echo $name." Dancing! ";
  }
}
$demo=new testC();
$demo->echostr();
$demo->dancing(" Model ");
// Running result 
/**
   Interface inheritance, to implement all related abstract methods 
   The model is dancing! 
**/

2. Inherit multiple interfaces


<?php
interface testA{
  function echostr();
}
interface testB{
  function dancing($name);
}
interface testC extends testA,testB{
  function singing($nickname);
}
class testD implements testC{
  function echostr(){
    echo " Interface inheritance, to implement all related methods of the parent interface! ";
    echo "<br />";
  }
  function dancing($name){
    echo $name." Dancing! ";
    echo "<br />";
  }
  function singing($nickname){
    echo $nickname." Singing! ";
  }
}
$demo=new testD();
$demo->echostr();
$demo->dancing(" Model ");
$demo->singing(" Jay Chou ");
// Running result 
/**
   Interface inheritance, to implement all related methods of the parent interface! 
   The model is dancing! 
   Jay Chou is singing! 
**/

tarits Multiple Inheritance

In multi-inheritance, one class can inherit multiple parent classes at the same time, and the function of combining multiple parent classes is to use this model to enhance the flexibility of integration in C + +. However, multi-inheritance is too flexible and will bring "diamond inheritance", so it is difficult to use it, and the model becomes complex. Now most languages give up the multi-inheritance model.
However, some occasions want to use more inheritance, but PHP does not inherit more, so it invented such a thing.

Traits can be understood as a set of methods that can be called by different classes, but Traits is not a class! Cannot be instantiated. Let's take an example to look at grammar:


<?php
trait myTrait{
  function traitMethod1(){}
  function traitMethod2(){}
}
// Then call this traits, The syntax is: 
class myClass{
  use myTrait;
}
// So that you can pass use myTraits That calls the Traits The method in, such as: 
$obj = new myClass();
$obj-> traitMethod1 ();
$obj-> traitMethod2 (); 
>

Specific introduction and use can see the official introduction.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "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: