gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php5.4之前版本怎么样使用非trait方法实现多继承功能

来源:http://erdangjiade.com/topic/827.html 你好,世界。 2017-09-25 22:42浏览(23)

像PHP,Java这类语言都只支持单继承,但有时候我要需要用到多继承,我可可以模拟出多继承,当然php5.4以后的trait也能更好的解决多继承问题。

下面我们介绍如何使用非trait方法实现多继承功能,

本段代码经本人测试可用,代码可供参考

代码如下:

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

以上就是本人实现的php多继承类的功能,代码仅供学习,正式项目中请使用php5.4的trait功能。

文章地址:http://www.erdangjiade.com/php-weizijiaocheng-377482.html

学编程就来PHP中文网 www.erdangjiade.com

~~~~

以上就是php5.4之前版本如何使用非trait方法实现多继承功能的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2