Re: Multiple Inheritance

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

 



On Wed, Feb 27, 2008 at 12:05 PM, chetan rane <chetan.d.rane@xxxxxxxxx>
wrote:

> Hi All
>
> Dose anyone know how to implement multiple inheritance in PHP 5.
> Interfaces dosent work . I have already Tried it.


the idea in a single inheritance language is to implement
'multiple inheritance' in the following way.
implement several interfaces an then compose a common
classes that implements said interfaces.  this is the workaround
for single inheritance languages.  here is a trivial example,

interface CommonStuff {
  function commonFunction($a, $b);
  function moreCommonFunction($x, $y, $z);
}

interface OftenUsed {
  function imSpecial($c, $d);
  function imMoreSpecial($u, $v, $w);
}

now you might have standard or common implementations
of these, sometimes referred to as mixins (taken from ruby,
i believe [<-- here i go again greg ;)])

class CommonStuffImpl implements CommonStuff {
  function commonFunction($a, $b) {
    return $a + $b;
  }

  function moreCommonFunction($x, $y, $z) {
    return $x + $y + $z;
  }
}

class OftenUsedImpl implements OftenUsed {
  function imSpecial($c, $d) {
    return $c - $d;
  }

  function imMoreSpecial($u, $v, $w) {
    return $u - $v - $w;
  }
}

and now finally for the multiple inheritance workaround;
suppose you have a class that needs to be both
CommonStuff and OftenUsed; you simply implement
both the interfaces, then delegate to the mixins, and
viola!  multiple inheritance the single inheritance way ;)

class MultiInherit implements CommonStuff, OftenUsed {
    private $commonStuffImpl = null;
    private $oftenUsedImpl = null;

    public function __construct() {
       $commonStuffImpl = new CommonStuffImpl();
       $oftenUsedImpl = new OftenUsedImpl();
    }

   /// now make sure to implement the interfaces and delegate
   function commonFunction($a, $b) {
      return $this->commonStuffImpl->commonFunction($a, $b);
   }

   function moreCommonFunction($x, $y, $z) {
      return $this->commonStuffImpl->moreCommonFunction($x, $y, $z);
   }

  function imSpecial($c, $d) {
    return $this->oftenUsedImpl->imSpecial($c, $d);
  }

  function imMoreSpecial($u, $v, $w) {
    return $this->oftenUsedImpl->imMoreSpecial($u, $v, $w);
  }
}

bear in mind i just typed that straight into my mail client, so it
might not be perfect, but you get the idea.
and also, dont forget about interface inheritance :)

interface Somethingable {
    public function doSomething();
}

interface Crazyfiable {
    public function getCrazy();
}

interface Extendable extends Somethingable, Crazyfiable {
    public function extend();
}

class SuperClass implements Extendable {
    public function doSomething() {
        echo 'i did something ..' . PHP_EOL;
    }

    public function extend() {
        echo 'i extended something..' . PHP_EOL;
    }

    public function getCrazy() {
        echo 'im a crazy bastard now!' . PHP_EOL;
    }
}

-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