php5.4之前版本如何使用非trait方法實現多繼承功能

來源:互聯網
上載者:User
像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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.