PHP interface multiple inheritance and tarits method to achieve multiple inheritance effect tutorial details

jacklove
Release: 2023-03-27 14:50:01
Original
1548 people have browsed it

The example of this article describes the method of multiple inheritance of PHP interface and tarits to achieve the effect of multiple inheritance. Share it with everyone for your reference, the details are as follows:

Multiple inheritance of interfaces

In PHP's object-oriented interface, interfaces can inherit interfaces. PHP classes can only inherit from one parent class (single inheritance), but interfaces can implement multiple inheritance and can inherit one or more interfaces. Of course, interface inheritance uses the extends keyword just like class inheritance. If you want multiple inheritances, just separate the inherited interfaces with commas.

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.

The following are examples:

1. Inherit a single interface

i

nterface testA{ 
function echostr(); 
}
interface testB extends testA{ 
function dancing($name); 
} 
class testC implements testB{ 
function echostr(){ 
echo "接口继承,要实现所有相关抽象方法!";
echo "
"; 
}
function dancing($name){
echo $name."正在跳舞!"; 
} 
}
$demo=new testC();
 
$demo->echostr();
 
$demo->dancing("模特");
 
//运行结果
 
/**
   
接口继承,要实现所有相关抽象方法
模特正在跳舞!
**/
Copy after login

2. Inherit multiple interfaces

interface testA{
 
function echostr();
 
}
 
interface testB{
 
function dancing($name);
 
}
 
interface testC extends testA,testB{
 
function singing($nickname);
 
}
 
class testD implements testC{
 
function echostr(){
 
echo "接口继承,要实现父接口所有相关方法!";
 
echo "
";
 
}
 
function dancing($name){
 
echo $name."正在跳舞!";
 
echo "
";
 
}
 
function singing($nickname){
 
echo $nickname."正在唱歌!";
 
}
 
}
 
$demo=new testD();
 
$demo->echostr();
 
$demo->dancing("模特");
 
$demo->singing("周杰伦");
 
//运行结果
 
/**
 
接口继承,要实现父接口所有相关方法!
 
模特正在跳舞!
 
周杰伦正在唱歌!
 
**/
Copy after login

taritsMultiple inheritance

In multiple inheritance, a class can inherit multiple parent classes at the same time. The function of combining multiple parent classes is this model used in C to enhance the flexibility of integration, but multiple inheritance It is too flexible and will bring about "diamond inheritance", so it is difficult to use and the model becomes complicated. Now most languages ​​​​have abandoned the multiple inheritance model.

But sometimes I want to use multiple inheritance, but PHP doesn’t have multiple inheritance, so I invented such a thing.

Traits can be understood as a set of methods that can be called by different classes, but Traits are not classes! They cannot be instantiated. Let’s take a look at the syntax with an example:

trait myTrait{
 
function traitMethod1(){}
 
function traitMethod2(){}
 
}
 
//然后是调用这个traits,语法为:
 
class myClass{
 
use myTrait;
 
}
 
//这样就可以通过use myTraits,调用Traits中的方法了,比如:
 
$obj = new myClass();
 
$obj-> traitMethod1 ();
 
$obj-> traitMethod2 (); 
>
Copy after login

The example in this article describes the method of multiple inheritance of PHP interface and tarits to achieve the effect of multiple inheritance. For more related knowledge, please pay attention to the PHP Chinese website.

Related recommendations:

Questions often encountered in PHP string reversal interviews

How to obtain the character stream in PHP The first non-repeating character in

PHP uses one line of code to delete all files in the directory.

The above is the detailed content of PHP interface multiple inheritance and tarits method to achieve multiple inheritance effect tutorial details. 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