Page 1 of 1

user classes and implementation

Posted: Sun Mar 15, 2009 8:24 pm
by take2hikes
Hi Everyone. I'm new to the forums and rather new to PHP. I recently bought some books on the subject and have began to plan out writing a blogging platform for personal use. Yes, I know that they are a dime a dozen.. but I'm doing it more for educational purposes. At least that's my excuse ;-).

About 6 years ago I took C and C++ classes but neither one was very advanced and the C++ class didn't deal with classes for very long. That combined with the intermission between then and now - and the result is that I'm pretty lost.

Keep in mind this is going to be a simple blog, at least at first. I have yet to code in the comment functionality, but I have bigger problems I need to address before that. The following is the Pseudo code I have for my two classes, DB and User.

DB Class:

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS DB
  // purpose: contains vars and functions necessary to connect to a
  //          database and send a query to it.
  //------------------------------------------------------------------------ 
  class DB
  {
      private $db;      // var to hold connection pointer
      private $query;   // var for query
  
      // constructor
      function __construct()
      { 
          // initialize all vars to null values
          $db = NULL;
          $query = NULL;
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars once class is out of scope
          unset($db);
          unset($query);      
      // end destructor
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION connect
      // purpose: protected function to connect to the database
      // required args: none
      //--------------------------------------------------------------------
      private function connect()
      {
          // define constants for connection info
          
          // open connection to database 
        
          // if unable to connect
          
              // return FALSE
    
          // end if 
     
          // else connection was successful
          
              // return TRUE
          
          // end else
           
      // end connect function 
      }
    
      //--------------------------------------------------------------------  
      // FUNCTION disconnect
      // purpose: protected function to disconnect to the database
      // required args: none
      //--------------------------------------------------------------------
      private function disconnect()
      {
          // if database closed successfully
          
              // return TRUE
              
          // end if
          
          // else connection wasn't closed
          
              // return FALSE
              
          // end else
          
      // end disconnect function
      }
              
      //--------------------------------------------------------------------  
      // FUNCTION query_db
      // purpose: protected function to send a query to the database
      // required args: query for database
      // optional args: true/false to return query result, ie SELECT query
      //--------------------------------------------------------------------
      private function query_db($query, $select=false)
      {
          // build SQL query from user input
          
          // if call to connect fails
          
              // return FALSE
              
          // end if
          
          // send query to database       
          
          // if query was successful              
            
              // if query should return data
              
                  // return result
                  
              // end if
              
              // return true
              
          // end if
          
          // else query wasn't successful
              
              // return false
              
          // end else          
                   
      // end query_db function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION get_recent_posts
      // purpose: private function to build a query to get the last 10
      //          recent posts by date, and pass the query to query_db
      // required args: number of posts to get
      //--------------------------------------------------------------------
      private function get_recent_posts($num_posts)
      {
          // build SQL query to get last (n) posts by date
          
          // call query_db with query
          
      // end get_recent_posts function
      }
    
      //--------------------------------------------------------------------  
      // FUNCTION get_user_posts
      // purpose: private function to build a query to get all posts from
      //          current user
      // required args: user_id from current User object
      //--------------------------------------------------------------------
      private function get_user_posts($user_id)
      {
          // build SQL query to get all posts made by current user
          
          // call query_db with query
             
      // end get_user_posts function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION del_user
      // purpose: private function to delete a user from the database. 
      //          function will only be called from a logged in user.
      // required args: User Object reference
      //--------------------------------------------------------------------
      private function create_user(&$userObj)
      {
          // build query with data from user object
          
          // if call to query_db fails
          
              // return FALSE
              
          // end if
          
          // else call to query_db was successful
          
              // return TRUE
              
          // end else
          
      // end create_user function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION user_login
      // purpose: private function to log user in
      // required args: user object reference
      //--------------------------------------------------------------------
      private function user_login(&$userObj)
      {          
          // build SQL SELECT query with email from user object
          
          // call query_db function with query and TRUE, assign return val
          
          // if return val was NOT FALSE, it must be a query result since we sent a SELECT
          
              // if entered pass is correct (same as pass stored for user in DB)
              
                  // populate all User vars with info from DB
                  
                  // if starting a new user session succeeds
          
                      // create new session vars with user_id and clearance 
              
                      // return TRUE
              
                  // end if
          
                  // else creating session failed
          
                      // return failure message
              
                  // end else   
                  
              // end if
              
              // else user pass did not match DB pass
                  
                  // return FALSE
                  
              // end else
              
          // end if
          
          
      // end user_login function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION del_user
      // purpose: private function to delete a user from the database. 
      //          function will only be called from a logged in user.
      // required args: current user object reference
      //--------------------------------------------------------------------
      private function del_user(&$userObj)
      {
          // build query with current user_id
          
          // if call to query_db with query is TRUE
          
              // return TRUE
              
          // end if
          
          // else call to query_db failed
          
              // return FALSE
              
          // end else      
              
      // end del_user function   
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION user_logout
      // purpose: private function to log user out of system
      // required args: current user object reference
      //--------------------------------------------------------------------
      private function user_logout(&$userObj)
      {
          // if user session is destroyed successfully
          
              // unset session vars
              
              // call destructor for user object
              
              // return TRUE
              
          // end if
          
          // else session couldn't be destroyed
          
              // return FALSE
              
          // end else        
          
      // end user_logout function
      }
      
  // end DB class
  }
 
And the User Class:

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS UserData
  // purpose: contains vars for all current user information
  //------------------------------------------------------------------------
  class UserData
  {
      
      private $user_id;            // current user ID
      private $user_name;          // current user name
      private $user_pass;          // current user pass
      private $user_email;         // current user email
      private $user_clearance;     // current user permissions
      private $user_createDate;    // current user creation date
      
       // constructor
      function __construct()
      { 
          // initialize all vars to null values
          $user_id = NULL;
          $user_name = NULL;
          $user_pass = NULL;
          $user_email = NULL;
          $user_clearance = NULL:
          $user_createDate = NULL;
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars once class is out of scope
          unset($user_id);
          unset($user_name);
          unset($user_pass);
          unset($user_eamil);
          unset($user_clearance);
          unset($user_createDate);      
      // end destructor
      }
      
      // set and get functions
      public function __set($name, $value)
      {
          $this->$name = $value;
      }
      public function __get($name)
      {
          return $this->$name;
      }    
       
  // end UserData class
  }
 
Now, I just made some changes and right now it's almost 230am, so if you see anything completely retarded.. I'm blaming the sleep deprivation.

For a while I had the user creation / deletion functions in the User class, but I since decided to go with it being more pure with just data. For all my connection/database needs I can pass the current User object or reference to the functions in the DB Class/Object.

Please.. constructively criticize me. I could make this 'work' numerous ways. I would like to do it using OOP and do it the RIGHT way. I'm trying to learn as much as possible from this, and I appreciate all of your input!

Re: user classes and implementation

Posted: Sun Mar 15, 2009 8:38 pm
by requinix
take2hikes wrote:For a while I had the user creation / deletion functions in the User class, but I since decided to go with it being more pure with just data. For all my connection/database needs I can pass the current User object or reference to the functions in the DB Class/Object.
Aww, and I prefer destructive criticism :)

The point of an object is to encapsulate stuff. If it's related to the object then the object should know about it.
So if there is something related to users then the User class should handle it. The database class shouldn't care about what it's being used for. The way you had it before was correct:

User creation, deletion, modification, and whatnot should be managed by the User class.
Database access should be managed by the DB class.

Re: user classes and implementation

Posted: Mon Mar 16, 2009 5:16 am
by take2hikes
Ok, so would you create a new DB object within the related User class function, say create_user(), to open up the connection and close it, or would you create the object outside of the function, open the connection, call the User object function, passing it the reference to the database connection, and then close the connection once the user function returns to you.. ?

Something like

Code: Select all

 
// create new objects
$myDB = new DB();
$currUser = new User();
 
// open database connection      
$myDB->connect();
 
// call create user function passing reference to DB connection and some other info    
$currUser->create_user(&$myDB->dbPointer, $email, $user);
 
// disconnect      
$myDB->disconnect();
 
Thanks for your response, it's appreciated.

Re: user classes and implementation

Posted: Mon Mar 16, 2009 8:51 am
by take2hikes
This is what I have so far, let me know what you guys think:

DB Class:

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS DB
  // purpose: contains vars and functions necessary to connect to a
  //          database and send a query to it.
  //------------------------------------------------------------------------ 
  class DB
  {
      private $db;      // var to hold connection pointer
      private $query;   // var for query
  
      // constructor
      function __construct()
      { 
          // initialize all vars to null values
          $db = NULL;
          $query = NULL;
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars once class is out of scope
          unset($db);
          unset($query);      
      // end destructor
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION connect
      // purpose: protected function to connect to the database
      // required args: none
      //--------------------------------------------------------------------
      private function connect()
      {
          // define constants for connection info
          
          // open connection to database 
        
          // if unable to connect
          
              // return FALSE
    
          // end if 
     
          // else connection was successful
          
              // return TRUE
          
          // end else
           
      // end connect function 
      }
    
      //--------------------------------------------------------------------  
      // FUNCTION disconnect
      // purpose: protected function to disconnect to the database
      // required args: none
      //--------------------------------------------------------------------
      private function disconnect()
      {
          // if database closed successfully
          
              // return TRUE
              
          // end if
          
          // else connection wasn't closed
          
              // return FALSE
              
          // end else
          
      // end disconnect function
      }
              
      //--------------------------------------------------------------------  
      // FUNCTION query_db
      // purpose: protected function to send a query to the database
      // optional args: true/false to return query result, ie SELECT query
      //--------------------------------------------------------------------
      private function query_db($select=false)
      {          
          // if call to connect fails
          
              // return FALSE
              
          // end if
          
          // send query to database       
          
          // if query was successful              
            
              // if query should return data
              
                  // return result
                  
              // end if
              
              // return true
              
          // end if
          
          // else query wasn't successful
              
              // return false
              
          // end else          
                   
      // end query_db function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION get_post
      // purpose: private function to build a query to retreive a single post
      //          using post_id. Will be used if user clicks on a single post
      //          to view comments.
      // required args: post_id for post to retreive
      // returns: Post object
      //--------------------------------------------------------------------
      private function get_post($post_id)
      {
          // build SQL SELECT query to get post from passed post ID
          
          // if call to query_db doesn't return FALSE, we sent a select so it must return something
          
              // create new Post object
          
              // assign post info from database to Post vars
              
              // return Post object
              
          // end if
          
          // else call to query_db returned FALSE
          
              // return FALSE
              
          // end else
      
      // end get_post function
      }    
      //--------------------------------------------------------------------  
      // FUNCTION get_recent_posts
      // purpose: private function to build a query to get the last 10
      //          recent posts by date, and pass the query to query_db
      // required args: number of posts to get
      //--------------------------------------------------------------------
      private function get_recent_posts($num_posts)
      {
          // build SQL query to get last (n) posts by date
          
          // if call to query_db doesn't return FALSE, we sent a select so it must return something
          
              // create (n) array of Post objects
          
              // assign post info from database to Post vars
              
              // return Post objects
              
          // end if
          
          // else call to query_db returned FALSE
          
              // return FALSE
              
          // end else
          
      // end get_recent_posts function
      }
    
      //--------------------------------------------------------------------  
      // FUNCTION get_user_posts
      // purpose: private function to build a query to get all posts from
      //          current user
      // required args: user_id from current User object
      //--------------------------------------------------------------------
      private function get_user_posts($user_id)
      {
          // build SQL query to get all posts made by current user
          
          // if call to query_db doesn't return FALSE, we sent a select so it must return something
          
              // create new array of Post object and assign posts to them
          
              // assign post info from database to Post vars
              
              // return Post objects
              
          // end if
          
          // else call to query_db returned FALSE
          
              // return FALSE
              
          // end else
             
      // end get_user_posts function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION edit_post
      // purpose: private function to build a query to edit a single post
      //          using a post object passed to it.
      //          to view comments.
      // required args: post object reference
      // returns: n/a
      //--------------------------------------------------------------------
      private function edit_post(&$postObj)
      {
          // build SQL query to delete post with current post id
          
          // if call to query_db returns TRUE
          
              // build SQL query to insert a new post with current post object info
          
              // if call to query_db returns TRUE
              
                  // return TRUE
                  
              // end if
                  
              // else if call to query_db returns FALSE
              
                  // return FALSE
                  
              // end else if
              
          // end if
          
          // else initial call to query_db returned FALSE
          
              // return FALSE
              
          // end else
      
      // end get_post function
      }    
    
  // end DB class
  }
 
UserData Class:

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS UserData
  // purpose: contains vars for all current user information
  //------------------------------------------------------------------------
  class UserData
  {
      
      private $user_id;            // current user ID
      private $user_name;          // current user name
      private $user_pass;          // current user pass
      private $user_email;         // current user email
      private $user_clearance;     // current user permissions
      private $user_createDate;    // current user creation date
      
      // constructor
      function __construct()
      { 
          // initialize all vars to null values
          $user_id = NULL;
          $user_name = NULL;
          $user_pass = NULL;
          $user_email = NULL;
          $user_clearance = NULL:
          $user_createDate = NULL;
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars once class is out of scope
          unset($user_id);
          unset($user_name);
          unset($user_pass);
          unset($user_email);
          unset($user_clearance);
          unset($user_createDate);      
      // end destructor
      }
      
      
      // set and get functions
      public function __set($name, $value)
      {
          $this->$name = $value;
      }
      public function __get($name)
      {
          return $this->$name;
      }    
      
   // end UserData class
  }
 
Post Class

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS Post
  // purpose: contains vars for post information
  //------------------------------------------------------------------------
  class Post
  {
      private $post_id;           // current post id
      private $user_id;           // user id associated with current post
      private $post_date;         // current post creation date
      private $post_subject;      // current post subject
      private $post;              // current post content 
      
      // constructor
      function __construct()
      { 
          // initialize all vars to null values
          $post_id = NULL;
          $user_id = NULL;
          $post_date = NULL;
          $post_subject = NULL;
          $post = NULL:
          
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars once class is out of scope
          unset($post_id);
          unset($user_id);
          unset($post_date);
          unset($post_subject);
          unset($post);  
              
      // end destructor
      }
      
      
      // set and get functions
      public function __set($name, $value)
      {
          $this->$name = $value;
      }
      
      public function __get($name)
      {
          return $this->$name;
      }    
  // end Post class
  } 
 
User Class

Code: Select all

 
//------------------------------------------------------------------------
  // CLASS User
  // purpose: contains functions for all user tasks.
  //------------------------------------------------------------------------ 
  class User
  {
      private $userData;                // will hold UserData object
      private $database;                // will hold DB object
      private $post;                    // will hold post for current user
      
      // constructor
      function __construct()
      { 
          $userData = new UserData();       // initialize a new UserData object
          $database = new DB();             // initialize a new DB object
          $post = new Post();               // initialize a new Post object
          
      // end constructor
      }
      // destructor
      function __destruct()
      {
          // unset all vars ( in this case objects ) once class is out of scope
          unset($userData);
          unset($database);
          unset($post);
               
      // end destructor
      }
      
         
      //--------------------------------------------------------------------  
      // FUNCTION del_user
      // purpose: private function to delete a user from the database. 
      //          function will only be called from a logged in user.
      // required args: n/a
      //--------------------------------------------------------------------
      private function create_user()
      {
          // build query in the DB object with data from user data object
          
          // if call to query_db fails
          
              // return FALSE
              
          // end if
          
          // else call to query_db was successful
          
              // return TRUE
              
          // end else
          
      // end create_user function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION user_login
      // purpose: private function to log user in
      // required args: n/a
      //--------------------------------------------------------------------
      private function user_login()
      {          
          // build SQL SELECT query in the DB object with email from user object
          
          // call query_db function and true so it returns SELECT result
          
          // if return val was NOT FALSE, it must be a query result since we sent a SELECT
          
              // if entered pass' md5 checksum is same as checksum stored for user in database
              
                  // populate all UserData vars with info from database
                  
                  // if starting a new user session succeeds
          
                      // create new session vars with user id and clearance 
              
                      // return TRUE
              
                  // end if
          
                  // else creating session failed
          
                      // return failure message
              
                  // end else   
                  
              // end if
              
              // else user pass did not match password in database
                  
                  // return FALSE
                  
              // end else
              
          // end if
          
      // end user_login function
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION del_user
      // purpose: private function to delete a user from the database. 
      //          function will only be called from a logged in user.
      // required args: n/a
      //--------------------------------------------------------------------
      private function del_user()
      {
          // build query in the DB object with current user_id
          
          // if call to query_db with query is TRUE
          
              // call destructor for user data object
          
              // return TRUE
              
          // end if
          
          // else call to query_db failed
          
              // return FALSE
              
          // end else      
              
      // end del_user function   
      }
      
      //--------------------------------------------------------------------  
      // FUNCTION user_logout
      // purpose: private function to log user out of system
      // required args: n/a
      //--------------------------------------------------------------------
      private function user_logout()
      {
          // if user session is destroyed successfully
          
              // unset session vars
              
              // call destructor for user data object
              
              // return TRUE
              
          // end if
          
          // else session couldn't be destroyed
          
              // return FALSE
              
          // end else        
          
      // end user_logout function
      }     
       
  // end User class
  }
 

I would try to incorporate try{}, throw, and catch functionality, but I will probably wait until I figure everything else out and get it coded in.