473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Class inheritance

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
11 3218
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

"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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
22
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
3
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.