Re: Copying an Object

From: Date: Wed, 22 Sep 2010 14:10:44 +0000
Subject: Re: Copying an Object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-308256@lists.php.net to get a copy of this message
On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <kolb0057@umn.edu> wrote: > Hello PHPers, > > I have: > > class A { > ...code > } > > class B extends A { > ...code > } > > $a = new A(); > > $b = new B(); > after looking back over your situation, which looks strange, why not use composition at this level, // note untested, may have some syntax errors class B { private $a = null; public function __construct(A $a) { $this->a = $a; } public function swapA(A $a) { $this->a = $a; } // proxy any calls to A which you would like to appear transparent public function __call($method, array $args) { return call_user_func_array(array($this->a, $method), $args); } // note requires 5.3 public function __callStatic($method, array $args) { return call_user_func_array(array($this->a, $method), $args); } } $a = new A(); $b = new B($a); // some code .. $anotherA = new A(); // some more code $b->swapA($anotherA); -nathan

« previous php.general (#308256) next »