Multiple Class Inheritance

Multiple Class Inheritance

am 27.01.2010 13:52:32 von Ashley Sheridan

--=-suD8Mnl5dYzsYEtmCbd6
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi All,

I know that a class can only inherit from one other single class in PHP,
but how would I go about simulating a multiple class inheritance? For
example, if I had several small classes that dealt with things like form
generation, navbar generation, etc, how could I create a class in which
these all existed?

Or am I thinking about this the wrong way? Should I have keep the
smaller classes, and have one larger object containing instances of each
as and how are necessary?

I've used classes before, but I'm fairly new to class inheritance, and
more particularly, the complex inheritance like I describe here.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-suD8Mnl5dYzsYEtmCbd6--

Re: Multiple Class Inheritance

am 27.01.2010 15:15:28 von Rene Veerman

If you want m-inheritance, you can "include" (encapsulate is the word
i think) your smaller classes in "midware" and "big top-level" classes
that expose (part of) their interfaces. It's easy.

But guard against creating too many dependencies between different
smaller classes to and "bigger" classes.

Logic extending the features of more than 1 smaller class for a
special use-case should be in a "bigger" class that loads instances of
the smaller classes.
Do consider building 3 to 5 layers of encapsulation, when 2 seems not
enough. A class that includes & extends features for 2 to 4 "small
classes" is better than a class that includes 50 "small classes" for
widely varying features.

Logic extending the features of just 1 smaller class, even for a
special use case used only once, should be placed inside the smaller
class (possibly activated by an $options=array(), or maybe by adding
"_descriptionOfUseCase" to the function-name. That may seem to lead to
bloat, but zend will take care of that.

Ultimately, classes should provide abstractions of top-, mid- and
lower-level areas of business-logic.
They "govern" a "problem-area".

Case in point; adodb.sf.net; it does database abstraction, for many
different server types, and nothing more than that.
It's "finished", because all the "use-cases" you'll ever encounter in
the "problem-area" have been coded into adodb.


On Wed, Jan 27, 2010 at 1:52 PM, Ashley Sheridan
wrote:
> Hi All,
>
> I know that a class can only inherit from one other single class in PHP,
> but how would I go about simulating a multiple class inheritance? For
> example, if I had several small classes that dealt with things like form
> generation, navbar generation, etc, how could I create a class in which
> these all existed?
>
> Or am I thinking about this the wrong way? Should I have keep the
> smaller classes, and have one larger object containing instances of each
> as and how are necessary?
>
> I've used classes before, but I'm fairly new to class inheritance, and
> more particularly, the complex inheritance like I describe here.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple Class Inheritance

am 27.01.2010 15:36:18 von Rene Veerman

Oh, and i'd allow 1 (or _maybe_ 2) "very big" super-class(es) at the
top level of a framework / cms, that do include 50-100 smaller
classes.

"midware" classes can evolve (be extracted) from the superclass, as
your app evolves.
Try to keep groups of functions relating to as few smaller/lower
classes as possible, to allow that to happen naturally.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple Class Inheritance

am 27.01.2010 15:47:42 von Ryan Sun

--001485eb9e32184bcf047e267c32
Content-Type: text/plain; charset=ISO-8859-1

1, you can implement multiple interfaces

2, you may want to return object instead of extending classes,
eg.
class Small_Class_Abstract
{
public function getFormGeneration()
{
return new Form_Generation();
}
}
class Small_Class_A extends Small_Class_Abstract
{
}
$A = new Small_Class_A();
$form = $A->getFormGeneration()->newForm();

On Wed, Jan 27, 2010 at 7:52 AM, Ashley Sheridan
wrote:

> Hi All,
>
> I know that a class can only inherit from one other single class in PHP,
> but how would I go about simulating a multiple class inheritance? For
> example, if I had several small classes that dealt with things like form
> generation, navbar generation, etc, how could I create a class in which
> these all existed?
>
> Or am I thinking about this the wrong way? Should I have keep the
> smaller classes, and have one larger object containing instances of each
> as and how are necessary?
>
> I've used classes before, but I'm fairly new to class inheritance, and
> more particularly, the complex inheritance like I describe here.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

--001485eb9e32184bcf047e267c32--

Re: Multiple Class Inheritance

am 27.01.2010 16:18:55 von Richard Quadling

2010/1/27 Ryan Sun :
> 1, you can implement multiple interfaces
>
> 2, you may want to return object instead of extending classes,
> eg.
> class Small_Class_Abstract
> {
>  public function getFormGeneration()
>  {
>    return new Form_Generation();
>  }
> }
> class Small_Class_A extends Small_Class_Abstract
> {
> }
> $A =3D new Small_Class_A();
> $form =3D $A->getFormGeneration()->newForm();
>
> On Wed, Jan 27, 2010 at 7:52 AM, Ashley Sheridan
> wrote:
>
>> Hi All,
>>
>> I know that a class can only inherit from one other single class in PHP,
>> but how would I go about simulating a multiple class inheritance? For
>> example, if I had several small classes that dealt with things like form
>> generation, navbar generation, etc, how could I create a class in which
>> these all existed?
>>
>> Or am I thinking about this the wrong way? Should I have keep the
>> smaller classes, and have one larger object containing instances of each
>> as and how are necessary?
>>
>> I've used classes before, but I'm fairly new to class inheritance, and
>> more particularly, the complex inheritance like I describe here.
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>

The Decorator pattern is an option here too I would guess.


--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple Class Inheritance

am 27.01.2010 16:23:37 von Nathan Nobbe

--001636e0b9c9895206047e26fcc7
Content-Type: text/plain; charset=UTF-8

On Wed, Jan 27, 2010 at 5:52 AM, Ashley Sheridan
wrote:

> Hi All,
>
> I know that a class can only inherit from one other single class in PHP,
> but how would I go about simulating a multiple class inheritance? For
> example, if I had several small classes that dealt with things like form
> generation, navbar generation, etc, how could I create a class in which
> these all existed?
>
> Or am I thinking about this the wrong way? Should I have keep the
> smaller classes, and have one larger object containing instances of each
> as and how are necessary?
>
> I've used classes before, but I'm fairly new to class inheritance, and
> more particularly, the complex inheritance like I describe here.
>

you should study the concept of composition as it pertains to OOP.

also, there are several threads in the archives regarding multiple
inheritance in PHP.

-nathan

--001636e0b9c9895206047e26fcc7--

Re: Multiple Class Inheritance

am 28.01.2010 01:58:52 von Nathan Rixham

Ashley Sheridan wrote:
> Hi All,
>
> I know that a class can only inherit from one other single class in PHP,
> but how would I go about simulating a multiple class inheritance? For
> example, if I had several small classes that dealt with things like form
> generation, navbar generation, etc, how could I create a class in which
> these all existed?
>
> Or am I thinking about this the wrong way? Should I have keep the
> smaller classes, and have one larger object containing instances of each
> as and how are necessary?
>
> I've used classes before, but I'm fairly new to class inheritance, and
> more particularly, the complex inheritance like I describe here.

"is a" and "has a" normally solves these composition vs inheritance
questions in a second or two.

perhaps a bit of studying on composition vs inheritance and common OO
design patterns would be suited; also consideration for separation of
cross cutting concerns | in fact a bit of reading up on java design
patterns would be recommended; it'll stand you in good stead.

the core J2EE Patterns catalog is well worth a look:
http://java.sun.com/blueprints/corej2eepatterns/index.html

Also Martin Fowler's pattern catalogue and indeed articles are worth
going over: http://martinfowler.com/eaaCatalog/

and for a general overview:
http://en.wikipedia.org/wiki/Category:Object-oriented_progra mming

finally, from the very limited details it sounds like implementing an
3-tier architecture where you stick application code in one tier and
presentation code in another would help abstract things a bit for you;
the classes you've mentioned sound very much like Utility or Helper
classes which would normally be called by methods which couple all the
functionality together. Each class and method needs to be considered on
a case by case basis; many will be able to be called statically and
thus no composition is needed; on occasions where you need to call
several methods from one instance within a single method then
instantiation within that method will suit; and when you need to access
instance based functionality across multiple methods in a class then
composition (or perhaps inheritance : see is-a has-a) will suit.

regards!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php