A very interesting special class was needed. How to assign '$ this' or how to work around this?

advertisements

I have a code similar to this one:

class A
{
   public function a()
   {
       echo "I'm at 'a' function of the class 'A'<br>";
   }

    public function b()
   {
       echo "I'm at 'b' function of the class 'A'<br>";
   }
   // ... and several other functions.

   public function z()
   {
       echo "I'm at 'z' function of the class 'A'<br>";
   }

}

class B
{
   public function a()
   {
       echo "I'm at 'a' function of the class 'B'<br>";
   }

    public function b()
   {
       echo "I'm at 'b' function of the class 'B'<br>";
   }
   // ... and several other functions.

   public function z()
   {
       echo "I'm at 'z' function of the class 'B'<br>";
   }

}

class Special
{
    public function construct($param)
    {
         //This code will not work. Is there an alternative?
         $this = new $param;
    }
}

$special = new Special("A");
$special->a();

$special = new Special("B");
$special->b();

Ouput:

    I'm at 'a' function of the class 'A'
    I'm at 'b' function of the class 'B'

The problem is I really would like write a class (at this case Special) that can performs methods from a class passed.

The only ugly way I could think to perform this is for each function that I have on A and B I write a code similar to this one:

public function h()
{
    // $param could be 'A' or 'B';
    $this->param->h();
}

But I really don't like to do this because to every function that I have on 'A' or 'B' class I will need to do this.

The mainly thing I would like is the Special class could run function as if it was the other class passed as argument on the construct method.

How can I countour this problem?


I see two ways to do this. First is more like a factory pattern and the second uses overloading.

Factory:

class Special
{
    public static function inst($param)
    {
         //This code will not work. Is there an alternative?
        return new $param;
    }
}

$special = Special::inst("A");
$special->a();

$special = Special::inst("B");
$special->b();

http://codepad.viper-7.com/LWl1Sn

http://en.wikipedia.org/wiki/Factory_method_pattern

In a factory pattern, you return the instance of the class from a static method. Then what the variable has really is a reference to the other classes, you are just using Special to make that instantiation.

The second method keeps the instantiation internal, but uses php's __GET, __SET and __CALL to get/set/call on that object.

class Special
{
    private $internal;

    public function __construct($param)
    {
         $this->internal = new $param;
    }

    public function __get($key)
    {
        return $this->internal->$key;
    }

    public function __set($key,$value)
    {
        $this->internal->$key=$value;
    }

    public function __call($method,$args)
    {
        return call_user_func_array(array($this->internal,$method),$args);
    }

}

$special = new Special("A");
$special->a();

$special = new Special("B");
$special->b();

http://codepad.viper-7.com/AlaR6D

Both should do the same thing with the factory most likely the preferred method.