


How to use non-trait methods to implement multiple inheritance in versions before php5.4
Languages like PHP and Java only support single inheritance, but sometimes I need to use multiple inheritance. Can I simulate multiple inheritance? Of course, traits after php5.4 can also better solve multiple inheritance. question.
Below we introduce how to use non-trait methods to implement multiple inheritance functions.
This code has been tested by myself and is available for reference.
The code is as follows:
<?php /* 多继承基类 注:要想实现类多继承,必需首先继承此基类 */ class multi_extends{ public function __construct(){ $this->_init_extends(); } protected function _init_extends(){ if(property_exists($this,'_extends')) { $extends=&$this->_extends; foreach($extends as $class) { $this->_extends($class); } } } public function _extends($class_name){ //分析目标 $ref = new ReflectionClass($class_name); //继承公共属性 $property_list=$ref->getProperties(ReflectionProperty::IS_PUBLIC); foreach($property_list as $property) { $property_name=$property->name; $property_value=$property->getValue(new $class_name); if($property_name==='_extends') { foreach($property_value as $c) { $this->_extends[]=$c; } }else{ if(!property_exists($this,$property_name)) { $this->{$property->name}=$property_value; } } } //继承公共方法 $method_list=$ref->getMethods(ReflectionMethod::IS_PUBLIC); foreach($method_list as $method) { $this->_extends_method[$method->name]=$method->class; } } function __call($m,$a){ if($c=$this->_extends_method[$m]) { eval("$c::$m(".'$a'.");"); } } } header('content-type:text/html;charset=utf-8'); class a{ public $a=5; public function say_name(){ echo '我叫'.$this->name.'<br>'; } } class b{ function say_age(){ echo '我今年'.$this->age.'岁<br>'; } } class c extends multi_extends{ public $_extends=array('a','b'); public function say_hellow(){ echo 'hellow '.$this->name.'<br>'; } } class d{ function say_goodbye(){ echo 'goodbye '.$this->name.'<br>'; } } class persion extends multi_extends{ public $_extends=array('c','d'); public $name="熊宝山"; public $age="20"; } //执行继承后的方法 $o=new persion(); $o->say_name(); $o->say_age(); $o->say_hellow(); $o->say_goodbye();
The above is the function of the PHP multiple inheritance class implemented by me. The code is for learning only. Please use the trait function of PHP5.4 in formal projects.
Article address: http://www.php.cn/php-weizijiaocheng-377482.html
Come to PHP Chinese website to learn programming www.php.cn
~~~~
The above is the detailed content of How to use non-trait methods to implement multiple inheritance in versions before php5.4. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPtraitDTO: Simplifying the development of data transfer objects Introduction: In modern software development, data transfer objects (DataTransferObject, referred to as DTO) play an important role. DTO is a pure data container used to transfer data between layers. However, during the development process, developers need to write a large amount of similar code to define and operate DTOs. In order to simplify this process, the trait feature was introduced in PHP. We can use the trait feature to

Friend functions allow non-member functions to access private members and play a role in multiple inheritance, allowing derived class functions to access private members of the base class.

In-depth understanding of the design patterns and practices of PHPtraitDTO Introduction: In PHP development, design patterns are an essential part. Among them, DTO (DataTransferObject) is a commonly used design pattern used to encapsulate data transfer objects. In the process of implementing DTO, using traits can effectively improve the reusability and flexibility of the code. This article will delve into the design patterns and practices of traitDTO in PHP

PHPtraitDTO: A key tool for optimizing the data transmission process. Specific code examples are required. Introduction: During the development process, data transmission is a very common requirement, especially when data is transferred between different levels. In the process of transmitting this data, we often need to process, verify or convert the data to meet different business needs. In order to improve the readability and maintainability of the code, we can use PHPtraitDTO (DataTransferObject) to optimize

PHPtraitDTO: Implementing simplicity and flexibility of data transfer objects Introduction: In the PHP development process, data transmission and processing are often involved. The DataTransferObject (DTO for short) is a design pattern that is used to transfer data between different layers. During the transmission process, DTO simplifies data operations by encapsulating data and providing public access methods. This article will introduce how to use PHPtrait to implement DT

Implementing a highly customizable data transfer framework using PHPtraitDTO As websites and applications become more complex, data transfer becomes more and more important. In PHP, using DataTransferObject (DTO for short) to handle data transfer can greatly simplify the code and improve maintainability and scalability. This article will introduce how to use PHPtrait and DTO to implement a highly customizable data transfer framework and provide corresponding code examples.

PHPtraitDTO: Elegant Data Transfer Object Pattern Overview: Data Transfer Object (DTO for short) is a design pattern used to transfer data between different layers. In applications, it is often necessary to obtain data from a database or external service and pass it between different layers of the application. The DTO mode can make data transmission more concise and clear, and also facilitates expansion and maintenance. In PHP, we can use traits to implement DTO

PHPtraitDTO: A key tool for optimizing the data transmission process. Specific code examples are required. In the development process, data transmission is a very critical link. How to transmit data efficiently has become one of the problems that developers need to solve. In PHP language, using traitDTO (DataTransferObject) can optimize the data transmission process and improve the efficiency of data transmission. This article will introduce what traitDTO is and how to use it to optimize the data transfer flow
