SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot TheMonk's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When should you use a OOP approach?

    I'm currently learning OOP in php and enjoying it, but I do have one question. Is there a general guideline that one can follow to determine if a particular problem should be solved in OOP vs procedural programming?

    So far I find one of PHP's strengths is it's ability to switch from OOP to procedural if the situation requires it. However how do you know when to make the switch to OOP?

    Thanks!

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any reasonably complex project can benefit from OOP. It can be much easier to maintain. If you are having trouble with file uploads for instance, and you are using a file upload class, you can take that class outside of the project on a test page, and pound on it a little, till you find the problem.

    Otherwise you are forced to wade through a lot of other code and perhaps filling out large numbers of required values in a form, just so you can test that one simple feature. Sometimes the bug is wider spread but if you are using mostly OOP, the problem is likely isolated.

    The other instance where OOP really shines is in cases where you find yourself writing the same code over and over with slight differences. That can get tired really quick.

    If you build that functionality into a class, you may never need to write that code again. Write an HTML table class and never have to write tabular data code for your database again. Just feed it the recordset and let it go.

    For that reason small projects can benefit too. You don't need a full blown MVC framework to do a simple contact form, but that is no reason to go back to procedural programming, if you can put together a nice validation/mail/spam protection class bundle, and deploy it in half the time, and move on.

    You can end up with a very nice re-usable and easily patched/upgradable toolbox for just about anything.

    There will be certain code that you will never get an error from again, because once the class has been debugged, you don't have to worry about that occasional mistake of forgeting a semicolon or curly brace because you are re-writing the code for the hundredth time, on auto pilot.

  3. #3
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've heard some people say turn anything into a class that can be feasibly turned into a class, but to me that seems heavy handed. Although I've only had a bit of experience with OOP I've found it really helpful when doing similar tasks again and again i.e. database access etc.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  4. #4
    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)
    OOP is a style of programming. You can use it all the time, and if that makes you feel comfortable, I think you should. Personally, I typically turn my procedural code into OOP, when it reaches more than about a screen in height.


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
  •