Re: Copying an Object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <kolb0057@xxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux