Dwayne wrote:[color=blue]
>
> Jerry Stuckle wrote:[color=green][color=darkred]
> >>>Aggregation is not inheritance.
> >>>
> >>
> >>True, but pragmatically, can you not achieve the same effect as mutliple
> >>inheritence this way? Or is there something I'm missing?[/color]
> >
> >
> > Nope. For instance - if you add or delete a method to A or B, you must
> > also change C. C is not a "type of" either A or B. This isn't as
> > important in PHP as other OO languages, but can cause problems, i.e. if
> > you use serialize().[/color]
>
> This is true, but you can dynamically bring methods and properties of
> other classes in, avoiding needing to manually update multiple classes:
>
> <?php
> class Foo
> {
> var $foo;
>
> function Foo()
> {
> $this->initFoo();
> }
>
> function getFoo()
> {
> return $this->foo;
> }
>
> function initFoo()
> {
> $this->foo = 1;
> }
> }
>
> class Bar
> {
> var $bar;
>
> function Bar()
> {
> $this->initBar();
> }
>
> function getBar()
> {
> return $this->bar;
> }
>
> function initBar()
> {
> $this->bar = 2;
> }
> }
>
> class FooBar
> {
> function FooBar()
> {
> aggregate_methods($this, 'Foo');
> aggregate_properties($this, 'Foo');
>
> aggregate_methods($this, 'Bar');
> aggregate_properties($this, 'Bar');
>
> $this->initFoo();
> $this->initBar();
> }
> }
>
> $foobar = new FooBar();
> print $foobar->getFoo();
> print $foobar->getBar();
> ?>
>
> Not that it isn't a bit clunky, and with it's own problems (constructors
> and private methods and properties not being brought in ('private' beind
> defined as something starting with an underscore)). The whole initFoo()
> initBar() thing isn't elegant, and strikes me as a potential nuisance
> down the line, but if you really need to simulate multiple-inheritance,
> it seems to me that it would work, with a bit of care taken.
>
> I have no idea how this would affect serialize(), though.
>
> Perhaps I was led astray by this bit in the PHP manual (yes, just
> pointing my finger and saying "but they did it too!")
>
> "In Object Oriented Programming, it is common to see the composition of
> simple classes (and/or instances) into a more complex one. This is a
> flexible strategy for building complicated objects and object
> hierarchies and can function as a dynamic alternative to multiple
> inheritance"
>
>
http://us2.php.net/manual/en/ref.objaggregation.php
>
> So I should have been more specific about this being aggregation rather
> than inheritance, but it seems to me the effect is close.
>[color=green]
> > Also, if you need to directly access a variable in
> > either A or B (not good programming practice, I admit - use accessor
> > methods instead), you need to know the internals of C. In short,
> > inheritance is transparent to the user. Aggregation is not.[/color]
>
> Agreed on all counts. I generally avoid accessing properties directly,
> even in child classes, which pretty much takes care of that problem.
> It's just another maintenance issue I prefer to avoid.
>[color=green]
> > Inheritance was created in part to resolve issues like this in
> > aggregation. I'm glad to see more OO techniques going into PHP. I
> > think it will only get better in this respect.[/color]
>
> Likewise...I use PHP because it works, but I think I'll actively enjoy PHP5.[/color]
This is true - you CAN do it this way. But again, you need to expost
the innards of FooBar. With inheritance, you really don't know if
FooBar is derived from Foo, Bar, both or neither. It's completely
transparent. And, in fact, with inheritance, you could create FooBar as
a standalone class - and later extract it into Foo, Bar and derive
FooBar from both. The result is 100% transparent to any application.
As I said - inheritance is great (if used properly). I've been using it
in C++ for around 16 years, and Java for 9 years. And I'm glad to see
PHP is implementing more if it, too.
--
To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
jstucklex@attglobal.net
Member of Independent Computer Consultants Association -
www.icca.org