By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Class inheritance

P: n/a
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Ricky Romaya wrote:
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA


Yep - check out Object Aggregation (mostly 4.2+)

http://us2.php.net/manual/en/ref.objaggregation.php

- Dwayne
Jul 17 '05 #2

P: n/a

"Ricky Romaya" <so*******@somewhere.com> wrote in message
news:Xn********************************@66.250.146 .159...
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA


Kind of. Example:

class A {
var $a;

function A($var) {
$this->a = $var;
}

function AFunk() {
echo "AFunk $this->a";
}
}

class B {
var $b;

function B($var) {
$this->b = $var;
}

function BFunk() {
echo "BFunk $this->b";
}
}

class C {

function C($a, $b) {
A::A($a);
B::B($b);
}

function AFunk() {
A::AFunk();
}

function BFunk() {
B::BFunk();
}
}

$obj = new C("Bear", "Tree");
$obj->AFunk();
$obj->BFunk();
Jul 17 '05 #3

P: n/a
Dwayne wrote:

Ricky Romaya wrote:
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA


Yep - check out Object Aggregation (mostly 4.2+)

http://us2.php.net/manual/en/ref.objaggregation.php

- Dwayne


Aggregation is not inheritance.

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #4

P: n/a
Chung Leong wrote:

"Ricky Romaya" <so*******@somewhere.com> wrote in message
news:Xn********************************@66.250.146 .159...
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA


Kind of. Example:

class A {
var $a;

function A($var) {
$this->a = $var;
}

function AFunk() {
echo "AFunk $this->a";
}
}

class B {
var $b;

function B($var) {
$this->b = $var;
}

function BFunk() {
echo "BFunk $this->b";
}
}

class C {

function C($a, $b) {
A::A($a);
B::B($b);
}

function AFunk() {
A::AFunk();
}

function BFunk() {
B::BFunk();
}
}

$obj = new C("Bear", "Tree");
$obj->AFunk();
$obj->BFunk();


It's a way to use aggregation to get around the lack of multiple
inheritance. The biggest problem is if you change A or B, you also have
to change C - something that inheritance gets around.

Additionally, you do not have a "type of" relationship - C is not a
"type of" either A or B. This isn't as important in PHP, which has very
little typing. But it can cause problems if you do something like
serialize() C.

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #5

P: n/a
Jerry Stuckle wrote:
Dwayne wrote:
Ricky Romaya wrote:
Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA


Yep - check out Object Aggregation (mostly 4.2+)

http://us2.php.net/manual/en/ref.objaggregation.php

- Dwayne

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?
Jul 17 '05 #6

P: n/a
No, that's not aggregation. There are no instances of A or B. $obj is
treated as though it's either. I would describe it as doing inheritance
manually. Not something I'd recommend, of course, since it's using an
undocument trick in the scripting engine.

Jul 17 '05 #7

P: n/a
Dwayne wrote:

Jerry Stuckle wrote:
Dwayne wrote:
Ricky Romaya wrote:

Hi,

Are there any ways to get multiple inheritace in PHP4? For example, I have
3 parent class, class A, B, and C. I want class X to inherit all those 3
classes. Consider merging those 3 classes is out of the question, as there
are class Y which only inherits from class A, etc.

TIA

Yep - check out Object Aggregation (mostly 4.2+)

http://us2.php.net/manual/en/ref.objaggregation.php

- Dwayne

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?


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(). 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.

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.

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #8

P: n/a
Jerry Stuckle wrote:
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?

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().


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.
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.
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.
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.


Likewise...I use PHP because it works, but I think I'll actively enjoy PHP5.

Jul 17 '05 #9

P: n/a
Dwayne wrote:

Jerry Stuckle wrote:
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?

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().


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.
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.


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.
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.


Likewise...I use PHP because it works, but I think I'll actively enjoy PHP5.

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.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #10

P: n/a
ch***********@hotmail.com wrote:

No, that's not aggregation. There are no instances of A or B. $obj is
treated as though it's either. I would describe it as doing inheritance
manually. Not something I'd recommend, of course, since it's using an
undocument trick in the scripting engine.


OK, now that I look more closely, I agree. But I also agree with not
recommending it. I hate using "undocumented feechures". :-)

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #11

P: n/a
Hmm, OK guys, I'd hate to reply my own post, but It seemed it's the best
way to reply to all your replies.

It seemed to me that all your replies hints the use of aggregation and
some kind of 'transparent container' technique. I'd have to agree with
Jerry about problems of modifying one or more parent class(es), which
have to be reflected in the 'inherited' class. I don't realy care about
serialize() for the moment, but this is what I'm afraid of. What if the
'inherited' class then serves as the parent of another class, or worse,
participates on yet another muliple inheritance scheme (i.e. class Z is
inherited from Class X, K, L, and class X is inherited from class A, B,
C)? (OK, I've heard many says that multiple inheritance are signs of bad
design, but what if I really need it)

Are there no middle ground here? I've heard that Ruby, a truly OO
language, also have single class inheritance restriction (like PHP), but
it 'simulates' multiple inheritance with 'mixins'. Anybody know about
this and care to elaborate on how to use 'mixins' in PHP4, and also the
pros and cons?

TIA
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.