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
}
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
}
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!