SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2009
    Location
    Ontario, Canada
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I do this in OOP? Help me understand the logic behind OOP.

    I currently am developing a webapp and I haven't used OOP techniques at all and I'm starting to get overwhelmed with unmanageable code. I've decided to convert this project into an OOP design. My problem is that I've never done OOP before so I don't quite understand the logic about when or what to make objects. I know what OOP is and how to make classes, I just don't know what objects to make.

    For example, look at my current webapp:



    Would you make one class called "Record" and make methods to do actions on the source, processing, bagging, grading and finished product stuff or do each of those go in their own class and objects with methods to do actions on each. I just don't understand where or what to make my objects.

    This will be my first time doing OOP. Hopefully someone can clear this up for me.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The most common way to do is is to create a PHP class per database table. Each of these classes then has the fields of the database as class fields. You can either choose to make them protected in the class and then use getters/setters to manipulate them, or just go with public fields and manipulate the values directly. The advantage of getters/setters is that you can do some gate keeping, i.e., raising errors like "The value of x must not exceed y" and stuff like that.

    Beside those classes you also need a mechanism to store/retrieve those objects from the database. The way I see this there are two ways you can do this.
    First, you can have all your classes extend a superclass and create all methods for storing/retrieving data in that superclass, or (second) you can create a separate class (or a bunch of classes that work together) whose responsibility it is to store/retrieve all data.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2009
    Location
    Ontario, Canada
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ouch, my current app has between 30 and 40 tables at any given time during development. Right now I'm at 33 again.


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
  •