Home  >  Article  >  Backend Development  >  How to simulate multiple inheritance in php

How to simulate multiple inheritance in php

不言
不言Original
2018-07-05 10:55:261925browse

This article mainly introduces how to implement simulated multiple inheritance in php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

How to implement simulated multiple inheritance in php

1. Summary

One sentence summary: In fact, when you inherit from others, you also want to call the methods and properties in other people's classes, so you can do this: create an object of the target class in this class, and then pass This object is used to call methods and properties, which is more convenient than inheritance.

2. Magic method __call implements simulated multiple inheritance

1. Related knowledge

PHP does not have the feature of multiple inheritance. Even in a programming language that supports multiple inheritance, we rarely use this feature. In most people's opinion, multiple inheritance is not a good design method. If you want to add additional features to a class, you don't necessarily need to use inheritance. Here I provide a method to simulate multiple inheritance for reference.

PHP has a magic method called __call. When you call a method that does not exist, this method will be automatically called. At this point, we have the opportunity to redirect the call to an existing method. For subclasses that inherit multiple parent classes, the process of finding methods is generally as follows:

My own method -> Method of parent class 1 -> Method of parent class 2...

The simulation process is roughly like this: Instantiate each parent class and then use it as an attribute of the subclass. These parent classes provide some public methods. When a subclass owns a method, the __call() function will not be called. This is equivalent to "overriding" the method of the parent class. When a method that does not exist is called, the __call() method is used to find methods that can be called from the parent class in turn. Although this is not complete multiple inheritance, it can help us solve the problem.

 1 <?php 
 2 class Parent1 { 
 3     function method1() {} 
 4     function method2() {} 
 5 } 
 6 class Parent2 { 
 7     function method3() {} 
 8     function method4() {}
 9 }
 10 class Child {
 11     protected $_parents = array();
 12     public function Child(array $parents=array()) {
 13         $_parents = $parents;
 14     }
 15      
 16     public function __call($method, $args) {
 17         // 从“父类"中查找方法
 18         foreach ($this->_parents as $p) {
 19             if (is_callable(array($p, $method))) {
 20                 return call_user_func_array(array($p, $method), $args);
 21             }
 22         }
 23         // 恢复默认的行为,会引发一个方法不存在的致命错误
 24         return call_user_func_array(array($this, $method), $args);
 25     }
 26 }
 27 $obj = new Child(array(new Parent1(), new Parent2()));
 28 $obj->method1();
 29 $obj->method3();

There is no inheritance of properties involved here, but it is not difficult to implement. Property inheritance can be simulated through the __set() and __get() magic methods. Please practice it yourself.

Other methods: Implement multiple inheritance through interfaces

Classes in php can only inherit one parent class. If you want to inherit multiple classes, you should use the interface

interface to simulate multiple Inheritance

3. Detailed explanation of multiple inheritance in PHP interface

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

It should be noted that when your interface inherits other interfaces, 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.

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("妈妈");  
?>

The code running results are as follows:

Interface inheritance, two abstract methods need to be implemented
The person who usually cooks is: Mom

The above example is an interface It inherits an interface, so when the test class implements the fam interface, it needs to instantiate two abstract methods, that is, instantiate both the abstract method of the interface's subclass and the parent class.

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("妈妈");  
?>

Example running results:

Interface inheritance needs to be implemented Two abstract methods
My name is: Xiaoqiang
The person who usually cooks is: Mom

This code is inherited due to the interface For two interfaces, all abstract methods of these three abstract classes must be instantiated 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 entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Operation mysqli preprocessing in PHP prepare

Initial introduction to PHP multi-process programming

Understanding of PHP polymorphism

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

Statement:
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