PHP - Object Oriented vs Procedural

Discussion in 'OT Technology' started by o2, Apr 10, 2007.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Can anyone tell me why/where I should use OO? Any websites that compare the 2 approaches?
     
  2. gnp

    gnp New Member

    Joined:
    Mar 11, 2007
    Messages:
    280
    Likes Received:
    0
    when the scope of the project becomes large and somewhat complicated, the initial outlay for good oo design is offset by the flexibilty and ease of maintenance.

    good oo design is not something you will pick up overnight. And once you do learn oo design, you will still need to learn at least a little uml and some basic design patterns before any of it will really pay off.
     
  3. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    Use OO basically everytime. The only time I would not use OO would be a quick-n-dirty one-time throw-away linear script.
     
  4. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Any good readings on this subject? Perhaps some good online comparison of the 2 methods?
     
  5. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    there's nothing to compare. If you are doing anything other than a quick-dirty, throw-away, never maintain script... Then you should use OOD.
     
  6. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Ive been using procedural method to maintain and add new features to my script almost weekly, and I dont find it a hassle or a problem. I just wanna know how OO will make things better....
     
  7. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    think of a task that you do a LOT. For example access a database. Or display a header/footer on your pages. Or a function that you use a lot. Now OOD allows you to write-once, and use many times.

    It also allows you to write your products to use generic functions that may run different back end code.

    For example, I have a script that must access a database. I need to run a query, and return a recordset. With OOD, I can create generic methods and then substitute different classes depending on the type of database: MSSQL, MySQL, Postgres, DB2, flat-file, etc. Without changing a single line in my code.

    By using it for presentation, such as header/footer/framework, you seperate the display from the function of the website. You also provide an "update once" "reflect everywhere" design. So I can update/change my framework in one class, and all pages loaded after that will use the new framework.
     
  8. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I thought about this for a while and I can't really think of anything except for ease of use when working with multiple coders. Is it any easier to maintain? Not really. I mean, to make a call to any other PHP file, whether it contains functions or an object you still need to include the source file.. when using an object you even have to take the additional step of creating a new object.

    For personal/in-house use I'd stick with procedures.

    If I were to share or publish code I'd probably go through it and OO it up.
     
  9. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,216
    Likes Received:
    521
    Any time you're working with "things", like images for example, your project probably lends itself to OO design.

    OO is good because it's easy to extend without breaking backwards compatibility, for example. You need to add new functionality to your Image class? No problem. All the old stuff still only uses the functions they were written to use. The new stuff won't break it.

    It's good for reusing code. You're writing another program that has to handle images? Just include Image.php and you don't have to reinvent the wheel.

    It's good for a whole lot of reasons. But if you don't want to be convinced, you don't need to. You can still use procedural design if you want. :dunno:
     
  10. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    What is the benefit of OO over putting everything into functions?

    Everything that I use more than once I put in functions.php, and simply call that function where I need it. A function for connection to db, a function for displaying the date (x hours ago, exact date, x days ago, etc), a function for getting the username from an id, etc.

    :dunno:
     
  11. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,918
    Likes Received:
    10
    Location:
    Los Angeles
    The main thing for me is re-usability.

    I wrote a Form Handler class a few years ago and have been using it ever since. Speaking of which, I should probably update it. It has automated error handling, verification, and has some nifty arguments to parse regular expressions through strings and characters. The point of it is, it never needs to be modified. I can copy the class over to any project I'm using, create the class object, and call the objects. It's so simple.

    I've yet to write any <form> related tags.

    To me, projects tend to be a lot "cleaner" when developing/designing from an object oriented standpoint. When you know how to write OO code effectively, you really won't ever need to modify the classes unless you're doing a really depth change.

    It's also more organized.
     

Share This Page