Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting site to OOP

    Hi all,

    I'm looking to get some theoretical help converting my website to OOP. Actually, I am re-doing the site entirely, so I'll be starting from scratch. This time around, I would like to make it as a dynamic OOP site (currently just static HTML) fetching content from the database.

    Now, I could very easily do this procedurally - in fact, I have done it many times before in the past. But I have recently been reading a lot on OOP and would like to adopt this practice. I'm just having a hard time figure out how how to construct my classes, how I should structure the database etc.

    Tasks I will need to perform:
    • Query database for content based on page ID or title
    • Return content
    • Create forms
    • Process forms


    Immediate classes that I can think of:
    • Database handler
    • Form class - create & process forms
    • Page class (?) content pulled from DB, also will be responsible for pulling content from DB


    For the backend, I will just use phpMysql admin, as it will only be me running the site. I may add something else later, if I decide to get fancy.

    Anyways, I am just not sure how to go about structuring my classes and getting them to interact.

    For example, say my Page class is in charge of receiving the page ID and looking up the resulting page content, how do I get it to interact with the database class? The database class would of course be responsible for making connection to the database, but would I need to pass this connection in for the Page class?
    ex:
    PHP Code:
    <?php
    class Page {
      
    set_id($id){
         
    $id=$id;
      }

      
    get_page_content($db object here?) {
       
    query db for content...
      }
    }
    ?>
    I just don't see it being practical having to pass the database object to every method call. Is the solution to pass it in the initialize call, or is there a better way?

    There is probably a lot of things I have not been clear about - most likely because I don't even know yet myself. Organizing my thoughts is already half of the battle!

    What would be some ideal methods for each class? I'm going to go hunting for pre-made classes across the web (of which I am sure there are a lot), but if anyone has any personal input for me, that would be great.

  • #2
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or maybe I am completely off with the example above.

    Maybe I want to retrieve the results using the database class (with a select query) and pass that result into the page class to be parsed for output as necessary, instead of querying inside of the page class.

    This would keep the 2 classes separate, and no need to constantly be passing db connection.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •