PHP good method writing different names but the same method class.

Source: Internet
Author: User
For example, {code...} A and B have different roles. They cannot be merged. The content of {code...} C and D is exactly the same. Is there any good way to write the content of C and D? Because it is backward compatible with 5.3, trait cannot be used ~~~ For example:

class A {    private $value = null;    public function __construct() {        $this->value = 1;    }}class B {    private $differentValue = null;    public function __construct() {        $this->differentValue = 1;    }}

The roles of A and B are completely different. They cannot merge.

class C extends A {    public function display() {        echo "OK";    }}class D extends B {    public function display() {        echo "OK";    }}

The content of C and D is exactly the same. Is there any good way to write the content of C and D only once?

Because it is backward compatible with 5.3, trait cannot be used ~~~

Reply content:

For example:

class A {    private $value = null;    public function __construct() {        $this->value = 1;    }}class B {    private $differentValue = null;    public function __construct() {        $this->differentValue = 1;    }}

The roles of A and B are completely different. They cannot merge.

class C extends A {    public function display() {        echo "OK";    }}class D extends B {    public function display() {        echo "OK";    }}

The content of C and D is exactly the same. Is there any good way to write the content of C and D only once?

Because it is backward compatible with 5.3, trait cannot be used ~~~

class A {private $value = null;public function __construct() {$this->value = 1;}}class B {private $differentValue = null;public function __construct() {$this->differentValue = 1;}}/*** @method void display()*/class C extends A {private $handler = null;public function __construct() {parent::construct();$this->handler = new E();}public function __call($name, $params) {if ( method_exists($this->handler, $name) ) {return call_user_func_array(array($this->handler, $name), $params);} else {throw new Exception('Method "'.$name.'" does not exists.');}}}/*** @method void display()*/class D extends B {private $handler = null;public function __construct() {parent::construct();$this->handler = new E();}public function __call($name, $params) {if ( method_exists($this->handler, $name) ) {return call_user_func_array(array($this->handler, $name), $params);} else {throw new Exception('Method "'.$name.'" does not exists.');}}}Class E {public function display() {echo "OK";}}

Http://3v4l.org/ns6q7


  value = 1;    }}class B{    public $name            = 'b';    private $differentValue = null;    public function __construct()    {        $this->differentValue = 1;    }}class CD{    private $parent = null;    public function __construct($parent)    {        $this->parent = new $parent;    }    public function __call($name, $params)    {        if ( is_callable(array($this->parent, $name)) ) {            return call_user_func_array(array($this->parent, $name), $params);        } else {            throw new Exception('Method "'.$name.'" should be callable.');        }    }    public function __get($property_name){        if(isset($this->$property_name)) {            return $this->$property_name;        } elseif (isset($this->parent->$property_name)) {            return $this->parent->$property_name;        } else {            return null;        }    }    public function display()    {        echo "OK";    }}$c = new CD(new B);var_dump($c->name);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.