SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question keeping track of subclasses

    hi, guys.

    I've just started using inheritance with PHP.

    my question is, how do you keep track of relationships between classes?
    meaning, if class C inherits from class B which inherits from class A, if you change something in class A it's gonna be hard to remember whether that'd break something in class C.

    is there some solution to this or it's not a problem at all?

    thanks!

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The solution is to limit how much you extend and use inheritance.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    The solution is to limit how much you extend and use inheritance.
    hi, thanks for replying.

    so is object-oriented programming really meant to deal with simple objects? for instance, objects that just hold/describe data, and don't really do more complicated stuff handling changes to the database?

    is this the standard way of doing this?

    thanks!

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not at all. There are languages that have nothing but OO.

    It shouldn't be a problem if you're using inheritance appropriately. If you do have a problem when changing a parent class, then the child class shouldn't have inherited in the first place, or the change you made shouldn't be in the parent class.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trapach View Post
    so is object-oriented programming really meant to deal with simple objects? for instance, objects that just hold/describe data, and don't really do more complicated stuff handling changes to the database?
    Not at all OOP can be very complex, but the goal of OO is to make a simple interface to the complex actions buried within.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rule of thumb: If in doubt, don't use inheritance. Anything that can be done with inheritance, can also be done with composition.

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, guys.

    it can be tricky, just have to get used to the inner workings, then with some experience it's gonna be all automatic, I won't even have to think that much about it.

    I use to use procedural code, and getting to understand how classes work - not the syntax but more how to use them correctly - took me some time, but now it's a no-brainer.

    just hope inheritance won't take me that much longer to grasp.

  8. #8
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trapach View Post
    hi, guys.

    I've just started using inheritance with PHP.

    my question is, how do you keep track of relationships between classes?
    meaning, if class C inherits from class B which inherits from class A, if you change something in class A it's gonna be hard to remember whether that'd break something in class C.

    is there some solution to this or it's not a problem at all?

    thanks!
    Two answers:

    1) It's called testing! bring in phpunit and write unit tests for your classes. Then when you make a change running the unit tests will prove you did/didn't shoot yourself in the foot.

    2) If changing something in one class is likely to break something in another class without your knowledge, then you're doing it wrong. Every class implements an interface which defines the way it interacts with the rest of the world. You should be able to do anything you want to that class so long as it doesn't impact the interface, without fear. If you document the interface well, you'll know when you're affecting other classes.

    Tools like phpdocumentor will map your class trees, and you can use it to track inheritance issues if you like, but again, good unit tests will catch those for you, immediately. It's what unit tests are for.

  9. #9
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arlen got it right.

    Write your tests before/same time as your classes, and keep them simple.
    Then, every time you change anything, run all your tests, and see what you broke, if anything.


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
  •