SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Should I Feel Bad?

    Hello, my fellow SitePointees!

    I was just facing an interesting dilemma with using classes in PHP4, and I was just curious if I handled it in the best way.

    I have been using this "events" class, that basically handles people registering for events. The "events" class extends to another class I have, "credit card" class, which is used to handle payment for events.

    Now, here is where I did some thinking: The "events" class also needs access to my "login" class for authenticating users, but PHP only supports single inheritance! I can't extend to another class! Well, I decided to be slick: I created a function within my "events" class, and I had the function create a new object from my "login" class. This solves my problem, but is it the best way to handle the problem? I guess so, but I just wanted some thoughts.
    Patrick Smith
    PHP Programmer

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it's called composition and its fine. Often favoured over complex inheritance.

  3. #3
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Complex Inheritance? Is that the same as multiple inheritance?
    Patrick Smith
    PHP Programmer

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    complex inheritance is not a technical term, or a name for a specific thing. complex was just the adjective I used in this case, but yeah I was thinking of multiple inheritance.

  5. #5
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since PHP doesn't support multiple inheritance, I guess the best way to go is composition. I'm just curious now if I should even use single inheritance in the first place.
    Patrick Smith
    PHP Programmer

  6. #6
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logitron View Post
    Since PHP doesn't support multiple inheritance, I guess the best way to go is composition. I'm just curious now if I should even use single inheritance in the first place.
    It would depend on the relationship between the objects, of course.

    If the new class is to be almost identical to a single class that already exists, minus maybe a new method or changing how a method is implemented, than single inheritance makes a ton of sense.

  7. #7
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that makes sense. Just need to use inheritance or composition on a case by case basis. I'll probably use composition most of the time, but I can see how inheritance can be valuable, too. Thanks for the help!
    Patrick Smith
    PHP Programmer


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
  •