PHP good way to write a different name but the same method of the class.

Source: Internet
Author: User
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, and neither of them can be combined.

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

The contents of C and D are identical, is there any good way to write the contents of C and D only once?

Because to be backwards compatible with 5.3, so trait can't use ~ ~ ~

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, and neither of them can be combined.

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

The contents of C and D are identical, is there any good way to write the contents of C and D only once?

Because to be backwards compatible with 5.3, so trait can't use ~ ~ ~

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))) {RE        Turn 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.