Home > Backend Development > PHP Tutorial > Analyze how PHP implements interface multiple inheritance (examples)

Analyze how PHP implements interface multiple inheritance (examples)

藏色散人
Release: 2023-04-10 08:24:01
forward
5683 people have browsed it

This article will analyze how PHP implements interface multiple inheritance (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

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 abstractions when implementing the interface. method.

The following are examples:

1. Inherit a single interface

<?php

interface testA{ 
	function echostr(); 
}  
interface testB extends testA{ 
	function dancing($name); 
}  
class testC implements testB{ 

	function echostr(){ 
		echo "接口继承,要实现所有相关抽象方法!"; 
		echo "<br>"; 
	}  

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

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 "接口继承,要实现父接口所有相关方法!";
		echo "<br />";
	}

	function dancing($name){
		echo $name."正在跳舞!";
		echo "<br />";
	}

	function singing($nickname){
		echo $nickname."正在唱歌!";
	}
}

$demo=new testD(); 
$demo->echostr(); 
$demo->dancing("模特");  
$demo->singing("周杰伦"); 

//运行结果
/**
	接口继承,要实现父接口所有相关方法!
	模特正在跳舞!
	周杰伦正在唱歌!

**/
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of Analyze how PHP implements interface multiple inheritance (examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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