What is the best method to merge two PHP objects?
You could create another object that dispatches calls to magic methods to the underlying objects. Here's how you'd handle __get
, but to get it working fully you'd have to override all the relevant magic methods. You'll probably find syntax errors since I just entered it off the top of my head.
class Compositor { private $obj_a; private $obj_b; public function __construct($obj_a, $obj_b) { $this->obj_a = $obj_a; $this->obj_b = $obj_b; } public function __get($attrib_name) { if ($this->obj_a->$attrib_name) { return $this->obj_a->$attrib_name; } else { return $this->obj_b->$attrib_name; } }}
Good luck.