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