SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 90

Thread: inheritance

  1. #1
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, PHP does support something resembling ruby mixins, which may suit your needs.

  6. #6
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please more

    Hello Firends
    Dr Livingston

    Please explain more,I'm interested

  7. #7
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  8. #8
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  12. #12
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  13. #13
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  14. #14
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  15. #15
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Selkirk,

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

    Thanks

  17. #17
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Suppose I should've googled first before asking huh?

    Anyways, it's appreciated...

  19. #19
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  20. #20
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

    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

  21. #21
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interface or mutiple inheritance?

    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.

  22. #22
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abalfazl
    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 .

  23. #23
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear lady

    Thank you very much for your answer

    Please explain by code.Then It will be more clear for me

  24. #24
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 IVehicleIFreight{} 
    In terms of polymorphism Cars and Trucks can both be IVehicle types, and Trucks can also be IFreight's.

  25. #25
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •