Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP > PHP Application Design
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Mar 15, 2005, 13:42   #1
abalfazl
PEACE WILL WIN
 
abalfazl's Avatar
 
Join Date: Feb 2005
Location: Beyond the seas there is a town
Posts: 504
inheritance

Hello firends

In C++,We can use multiple inheritance.

But in PHP 5,A class can inherit from only one parent class.What is advantage of this?
abalfazl is online now   Reply With Quote
Old Mar 15, 2005, 13:45   #2
seanf
Mlle. Ledoyen
silver trophy
 
seanf's Avatar
 
Join Date: Jan 2001
Location: UK
Posts: 7,312
Thread moved. This question is more suited to this forum

Sean
seanf is offline   Reply With Quote
Old Mar 15, 2005, 13:51   #3
sweatje
eschew sesquipedalians
silver trophy
 
sweatje's Avatar
 
Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,779
Simplicity. If you have multiple inheritance, and you inherit from two or more classes which define the same method, which one do you use?

Java got out of that particular nightmare by providing support for interfaces, a model which PHP5 has adopted as well.
sweatje is offline   Reply With Quote
Old Mar 15, 2005, 14:40   #4
Dr Livingston
Non-Member
 
Join Date: Jan 2003
Posts: 5,799
Composition is an alternative approach to take, to get around Multiple Inheritance, and offers far more advantages to you by the way

I could explain some more if your interested...
Dr Livingston is offline   Reply With Quote
Old Mar 15, 2005, 14:47   #5
sweatje
eschew sesquipedalians
silver trophy
 
sweatje's Avatar
 
Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,779
BTW, PHP does support something resembling ruby mixins, which may suit your needs.
sweatje is offline   Reply With Quote
Old Mar 15, 2005, 22:52   #6
abalfazl
PEACE WILL WIN
 
abalfazl's Avatar
 
Join Date: Feb 2005
Location: Beyond the seas there is a town
Posts: 504
Please more

Hello Firends
Dr Livingston

Please explain more,I'm interested
abalfazl is online now   Reply With Quote
Old Mar 16, 2005, 05:42   #7
timvw
SitePoint Addict
 
timvw's Avatar
 
Join Date: Jan 2005
Location: Belgium
Posts: 355
well... assume you have a class AB that has as parents A and B.

with composition you can define AB as:

class AB extends A (could be B also)
{
private $_b;

public function __construct()
{
$this->_b = new b;
}

// now implement all b-methods
public function aBMethod
{
return $this->_b->BMethod();
}
}

i assume that with __call overloading things are even easier..
sometimes you don't want to inherit from one class, then you'll have a class AB that has a A and a B... enough possibilities to work-around the single-inheritance...
timvw is offline   Reply With Quote
Old Mar 16, 2005, 08:34   #8
MiiJaySung
Resident Java Hater
 
Join Date: Jul 2004
Location: Gerodieville Central, UK
Posts: 479
In C++ you sort of need Multiple Inheritance for various tasks. For instance, if you want to use C++ interfaces with Pure Virtual Functions, you are forced to using classes as there is no direct support for interfaces. If you have a set of classes containing PVM's which want to implement in a given class you need MI to aggregate these interfaces.

C++ being a compiled / strict typed language obviously doesn't have the reflective properties of VM based languages like Java, ruby, & PHP. In fact early versions of the language didn't have any constistent form of the RTTI. Reflection often lets you inspect data types at runtime, which is needed if you want to aggregate classes using things like mixins / dynamic proxies / __call() overloading. In it's current state C++ lacks the level of reflection used by modern languages.

Reflection allows us to avoid MI in favour of mixins, hence removing the complex rules that MI gives us, and giving the programmer more control instead of making the programmer work round the complex rules of C++ MI (Which gets very messy when you take into account things like different pointers, virtual methods, and the different array of casts, and templates).

Remember also, PHP is a language to quickly make simple maintainable web pages. it's not designed like C++ to do just about anything after a load of hair loss.
MiiJaySung is offline   Reply With Quote
Old Mar 16, 2005, 08:37   #9
MiiJaySung
Resident Java Hater
 
Join Date: Jul 2004
Location: Gerodieville Central, UK
Posts: 479
Quote:
Originally Posted by Dr Livingston
Composition is an alternative approach to take, to get around Multiple Inheritance, and offers far more advantages to you by the way

I could explain some more if your interested...
Exactly. Composition gives you total control, where as MI replies on complex compiler rules. It just means you need to think more about your design instead of assume that the compiler will do all the magic for you.
MiiJaySung is offline   Reply With Quote
Old Mar 16, 2005, 11:38   #10
Dr Livingston
Non-Member
 
Join Date: Jan 2003
Posts: 5,799
Enough said then... Phew Don't like big speeches myself, and I'm never the one looking for an audiance either

On the other hand though, if you need an indepth explamation, search for Multiple Inheritance over at www.javaworld.com

This is where I started to poke around.
Dr Livingston is offline   Reply With Quote
Old Mar 16, 2005, 14:29   #11
arborint
SitePoint Wizard
 
Join Date: Aug 2004
Location: California
Posts: 1,672
It is interesting how attitudes have changed on inheirtance. I think that progress in XP and TDD has had a big influence on it. Deep inheirtance, whatever its technical value, is not in the spirit of today's methodologies. Composition seems to have proven itself to be more flexible and testable, or at least feels that way. It has almost come full circle where inheirtance is now applied as Static Composition.
arborint is offline   Reply With Quote
Old Mar 28, 2005, 12:29   #12
lastcraft
SitePoint Victim
 
lastcraft's Avatar
 
Join Date: Apr 2003
Location: London
Posts: 2,273
Hi...

Quote:
Originally Posted by arborint
It is interesting how attitudes have changed on inheirtance. I think that progress in XP and TDD has had a big influence on it.
More likely the patterns movement? Three of the gang of four were C++ers and the Visitor explanation for example is built around C++ overloading rules. Yet their manatra was "favour composition over inheritance". As a side note, the book "Holub on Patterns" (mainly Java focused) has a lot of discussion about the composition over inheritance shift that has happened since the 80's.

yours, Marcus
lastcraft is offline   Reply With Quote
Old Mar 28, 2005, 14:35   #13
arborint
SitePoint Wizard
 
Join Date: Aug 2004
Location: California
Posts: 1,672
Interesting, and thanks for the book reference.

I think you are right about the patterns movement. A decade or two of experience building real applications certainly _field tested_ these concepts. And the fact that the platforms and even the types of applications have changed over the last decade has certainly had an effect as well.
arborint is offline   Reply With Quote
Old Mar 29, 2005, 07:47   #14
sweatje
eschew sesquipedalians
silver trophy
 
sweatje's Avatar
 
Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,779
I just finished reading Holub on Patterns during my "vacation" Overall, I think the book is very solid, and did a good job of living up to it's sub-title of "explaining patterns through looking at code". The two examples were a Game of Life implementation and an embeded database which stored data in csv files and supported a SQL layer. The game of life example had a bit of "Patternitis" but was still very good. I think the db example was very realistic.

Two things I think are great take aways which I may use in my upcomming talk on Patterns at php|tropics are the term "reification" and the concept of a UML pattern diagram superimposed on an abbreviated class diagram to show pattern relationships. This is very interesting, especially in the light of a) realistic naming (i.e. your class names should reflect their role in the domain model, not an arbitrary pattern name) and b) classes can be colaborators in more than one pattern at a time.

Anyway, thumbs up from me on this book as well.
sweatje is offline   Reply With Quote
Old Mar 29, 2005, 15:41   #15
Selkirk
SitePoint Guru
 
Join Date: Nov 2002
Posts: 848
Quote:
Originally Posted by sweatje
This is very interesting, especially in the light of a) realistic naming (i.e. your class names should reflect their role in the domain model, not an arbitrary pattern name) and b) classes can be colaborators in more than one pattern at a time.
I agree that Naming is important. I just wanted to point out the single responsibility principle and interface segregation principle from that post in light of this thread.
Selkirk is offline   Reply With Quote
Old Mar 29, 2005, 16:11   #16
Dr Livingston
Non-Member
 
Join Date: Jan 2003
Posts: 5,799
Selkirk,

I'm aware of the single responsibility principle but not the other principle? Do you have a link to more information...

Thanks
Dr Livingston is offline   Reply With Quote
Old Mar 29, 2005, 18:59   #17
Selkirk
SitePoint Guru
 
Join Date: Nov 2002
Posts: 848
Sure: Interface Segregation Principle.
Selkirk is offline   Reply With Quote
Old Mar 29, 2005, 20:25   #18
Dr Livingston
Non-Member
 
Join Date: Jan 2003
Posts: 5,799
Thanks. Suppose I should've googled first before asking huh?

Anyways, it's appreciated...
Dr Livingston is offline   Reply With Quote
Old Mar 29, 2005, 20:42   #19
ethanp
Floridian Opera Brigadier
 
ethanp's Avatar
 
Join Date: Jul 2004
Location: Florida
Posts: 1,975
I think that C++ and multiple inheritance is an effective method for a variety of task, and I don't see a reason not to support it. Simply using a similar structure to C++'s method wouldn't hurt a bit and provide extended programming options.

PHP took too much out of OOP for someone coming from C++. I think multiple inheritance is one, but I'll grow away from it as it doesn't look like PHP is going to support it any time soon.
ethanp is offline   Reply With Quote
Old Mar 29, 2005, 20:58   #20
Dr Livingston
Non-Member
 
Join Date: Jan 2003
Posts: 5,799
Quote:
I think that C++ and multiple inheritance is an effective method for a variety of task, and I don't see a reason not to support it.
No.... .... .... You don't want -BEEP- -BEEP- -BEEP- Multiple Inheritance...

Quote:
Simply using a similar structure to C++'s method wouldn't hurt a bit and provide extended programming options.
Wouldn't hurt huh? Well, I'd tend to disagree with you, and no Multiple Inheritance does not provide any options, but what does provide options is Interfaces. This is why Java supports Interfaces, and this is why Java is the better langauge for it
Dr Livingston is offline   Reply With Quote
Old Apr 22, 2005, 01:50   #21
abalfazl
PEACE WILL WIN
 
abalfazl's Avatar
 
Join Date: Feb 2005
Location: Beyond the seas there is a town
Posts: 504
Interface or mutiple inheritance?

Hello

It's said:

Quote:
It's quite possible to do the same things that are done with multiple inheritance through interfaces (which PHP5 supports) and advanced OO patterns.

I wanna know how can interface that

What are advantages of using interface instead of Multiple inheritance?(Please give me an example)

Last edited by abalfazl; Apr 22, 2005 at 03:41.
abalfazl is online now   Reply With Quote
Old Apr 22, 2005, 02:33   #22
laura2005
SitePoint Addict
 
Join Date: Mar 2005
Posts: 214
Quote:
Originally Posted by abalfazl
Quote:
It's quite possible to do the same things that are done with multiple inheritance through interfaces (which PHP5 supports) and advanced OO patterns.
Hello

It's said:

Hello


I wanna know how can interface that

What are advantages of using interface instead of Multiple inheritance?(Please give me an example)
Answer here: inheritance = tight coupling
http://www.sitepoint.com/forums/showthread.php?t=252133

Haven't looked at PHP5 : it's great if it supports interfaces but my hosting provider doesn't support PHP5 yet .
laura2005 is offline   Reply With Quote
Old Apr 22, 2005, 03:47   #23
abalfazl
PEACE WILL WIN
 
abalfazl's Avatar
 
Join Date: Feb 2005
Location: Beyond the seas there is a town
Posts: 504
Dear lady

Thank you very much for your answer

Please explain by code.Then It will be more clear for me
abalfazl is online now   Reply With Quote
Old Apr 22, 2005, 05:36   #24
Shrike
SitePoint Zealot
 
Join Date: Jan 2005
Location: United Kingdom
Posts: 199
I am still learning the use of interfaces, but here is an example - sorry if it is nonsense

We write a class called Car. This implements an interface IVehicle which gives some basic methods about a Vehicle. We could use an abstract class here but we use an interface because it allows more flexibility.
PHP Code:

interface IVehicle{}

class
Car implements IVehicle{}
$car = new Car;
$car->implementedMethodFromIVehicle();
Now we introduce a new class Truck. A truck is also a vehicle so it implements IVehicle.
PHP Code:

class Truck implements IVehicle{} 

Ok great, except a Truck carries freight and we need to know what that is. So with a single interface/inheritance we are stuck with writing a new method in IVehicle to cope with this. But then Car is broken, and so must be rewritten. With multiple interfaces we can just write a new interface IFreight and have Truck implement that as well, and Car objects continue to work.
PHP Code:

interface IFreight{}

class
Truck implements IVehicle, IFreight{}
In terms of polymorphism Cars and Trucks can both be IVehicle types, and Trucks can also be IFreight's.
Shrike is offline   Reply With Quote
Old Apr 22, 2005, 09:01   #25
laura2005
SitePoint Addict
 
Join Date: Mar 2005
Posts: 214
Quote:
Originally Posted by abalfazl
Dear lady

Thank you very much for your answer

Please explain by code.Then It will be more clear for me
The answer above was for Java/JSP forum. Any java programmer obligatory knows the code for interfaces as interfaces are everywhere in Java API they cannot write any program without using them so I didn't need to give any code. And that's the point though they know how to code interfaces which is simplistic, it doesn't prevent some not to say many of them not to understand the interest of interfaces.

When you will code interfaces you will be faced with the "bureaucracy" of interfaces you will have to deal with supplemental interfaces variables declarations. You could be fed up and wonder if it's worth the pain then only you would perhaps need to remember the above answer . So at the moment you can just forget it if all you need is the syntax - which has been given above so I don't need to thanks Shrike .

Now I'm not sure of the real interest of interfaces just for generating web pages, normally it's rather used for business objects like the EJB in Java. In fact I suspect that since SUN endorsed PHP recently they need that PHP programmers get used to some programming paradigms necessary to attack java interfaces from PHP: SUN business is to sell Business Application Servers that's where the value in $ is, they care less about JSP containers which can be found for free and as JSP is not as widespread as PHP strategically it's well done.
laura2005 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 23:26.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved