Home >Backend Development >PHP Tutorial >How to use non-trait methods to implement multiple inheritance in versions before php5.4

How to use non-trait methods to implement multiple inheritance in versions before php5.4

PHP中文网
PHP中文网Original
2017-08-26 10:13:391550browse

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,&#39;_extends&#39;))
                {
                        $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===&#39;_extends&#39;)
                        {
                                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(".&#39;$a&#39;.");");
                }
        }
}
header(&#39;content-type:text/html;charset=utf-8&#39;);
class a{
        public $a=5;
        
        public function say_name(){
                echo &#39;我叫&#39;.$this->name.&#39;<br>&#39;;
        }
}
class b{
        function say_age(){
                echo &#39;我今年&#39;.$this->age.&#39;岁<br>&#39;;
        }
        
}
class c extends multi_extends{
        public $_extends=array(&#39;a&#39;,&#39;b&#39;);
        
        
        public function say_hellow(){
                echo &#39;hellow &#39;.$this->name.&#39;<br>&#39;;
        }
}
class d{
        function say_goodbye(){
                echo &#39;goodbye &#39;.$this->name.&#39;<br>&#39;;
        }
        
}
class persion extends multi_extends{
        public $_extends=array(&#39;c&#39;,&#39;d&#39;);
        
        
        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!

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