Tony Marston wrote:
Once you have extended a (parent) class and created a (child) subclass it is
simply not possible to add another parent to that child class. This applies
to all OO languages, not just PHP. Each child class can have only one
parent.
Tony, I hope this is not what you meant ? Many (non cryptic) OOPLs offer
multiple inheritence (C++, Python, Common Lisp...).
<op>
Now it's true that PHP only support single inheritence. But inheritence
is far from being the alpha and omega of OOP. Aggregation|com position +
delegation is another way to reuse code (ok, this means having more code
to write, but what, this is straightforward ...)
ex :
class Parent
{
function Parent($args) { ... }
function dothis($args) { ... }
}
class Mixin
{
function dothat($withit) { ... }
}
class Child extends Parent
{
function Child($args)
{
parent::Parent( $args);
[...]
$this->mixin =& new Mixin();
}
function dothat($withit)
{
return $this->mixin->dothat($withit );
}
}
Now you can use Child as either a Parent object, a Child object, or a
Mixin object...
One advantage of aggregation|com position ws inheritence is that it can
be dynamic :
class Mixin2
{
function dothat($withit) { [not the same code as Mixin's] }
}
class Child2 extends Parent
{
function Child($args, $mixinKlassname )
{
parent::Parent( $args);
[...]
$this->mixin =& new $mixinKlassname ();
}
function dothat($withit)
{
return $this->mixin->dothat($withit );
}
}
c2one =& new Child2($args, 'Mixin');
c2two =& new Child2($args, 'Mixin2');
HTH
Bruno