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?
| SitePoint Sponsor |




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?
Thread moved. This question is more suited to this forum
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
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.
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.





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...
BTW, PHP does support something resembling ruby mixins, which may suit your needs.




Hello Firends
Dr Livingston
Please explain more,I'm interested


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



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.



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.Originally Posted by Dr Livingston





Enough said then... PhewDon'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.





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





Hi...
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.Originally Posted by arborint
yours, Marcus
Marcus Baker
Testing: SimpleTest, Cgreen, Fakemail
Other: Phemto dependency injector
Books: PHP in Action, 97 things





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.
Christopher
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.
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.




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.Originally Posted by sweatje





Selkirk,
I'm aware of the single responsibility principle but not the other principle? Do you have a link to more information...
Thanks![]()









Thanks. Suppose I should've googled first before asking huh?
Anyways, it's appreciated...





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.





No.... .... .... You don't want -BEEP- -BEEP- -BEEP- Multiple Inheritance...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.
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 itSimply using a similar structure to C++'s method wouldn't hurt a bit and provide extended programming options.![]()




Hello
It's said:
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 02:41.


Answer here: inheritance = tight couplingOriginally Posted by abalfazl
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.




Dear lady
Thank you very much for your answer
Please explain by code.Then It will be more clear for me


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.
Now we introduce a new class Truck. A truck is also a vehicle so it implements IVehicle.PHP Code:interface IVehicle{}
class Car implements IVehicle{}
$car = new Car;
$car->implementedMethodFromIVehicle();
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:class Truck implements IVehicle{}
In terms of polymorphism Cars and Trucks can both be IVehicle types, and Trucks can also be IFreight's.PHP Code:interface IFreight{}
class Truck implements IVehicle, IFreight{}


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.Originally Posted by abalfazl
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.
Bookmarks