Members class-OO: Suggestions solicited[Unsolved]
Posted: Thu Nov 03, 2005 2:33 am
[NOTE: Scroll down to the bottom of this page for recent problems]
Hello all,
I have a Members_tbl which has the following fields
This is the first time I am going in for total OO development.
I have developed three classes for the Members_tbl
1. Members
2. MemberCreation
3. MemberRemoval
1. Do you think that I need three classes for that?
2. What do you think of the implementation...is there a better idea available?
3. Can you suggest better code commenting(classes, functions and small code blocks) practices as well?
4. Is it necessary to store even small classes in individual files with the same names as that of respective classes?
Members Class
MemberRemoval Class
Hello all,
I have a Members_tbl which has the following fields
Code: Select all
MemberId, UserName, Password, PrivelegeId, FirstName, LastName, Age,
Gender, Email, Phone, Street, City, State, Country, PostCode, JoinDateI have developed three classes for the Members_tbl
1. Members
2. MemberCreation
3. MemberRemoval
1. Do you think that I need three classes for that?
2. What do you think of the implementation...is there a better idea available?
3. Can you suggest better code commenting(classes, functions and small code blocks) practices as well?
4. Is it necessary to store even small classes in individual files with the same names as that of respective classes?
Members Class
Code: Select all
class Members{
/**
* @member_array contains the following fields from Members_tbl
* MemberId, UserName, Password, PrivelegeId, FirstName, LastName, Age,
* Gender, Email, Phone, Street, City, State, Country, PostCode, JoinDate
* @databaseConnection: Database connection handler
*/
var $member_array = array();
var $db_connection;
/**
* Constructor of the Members class
* 1. The member id is received as input and all the field values are retrieved and stored into class variables
* @member_id: gets the id of the member
*/
function Members($member_id, $db_connection){
$this->db_connection = $db_connection;
$this->member_array["member_id"] = $member_id;
set_member_variables();
}
/**
* Sets the values for all the class member variables by retrieving all the values from the database for the member id given
* 1. Use the member variable $member_id to retrieve values from database and set them for other member variables
*/
function set_member_variables(){
$query = "select * from `Members_tbl` where `MemberId` = {$this->member_id}";
$statement = $this->db_connection->executeQuery($query);
if ($statement->findRows() == 1){
$row = mysql_fetch_assoc($statement->result);
$this->member_array["UserName"] = $row["UserName"];
$this->member_array["Password"] = $row["Password"];
$this->member_array["PrivelegeId"] = $row["PrivelegeId"];
$this->member_array["FirstName"] = $row["FirstName"];
$this->member_array["LastName"] = $row["LastName"];
$this->member_array["Age"] = $row["Age"];
$this->member_array["Gender"] = $row["Gender"];
$this->member_array["Email"] = $row["Email"];
$this->member_array["Phone"] = $row["Phone"];
$this->member_array["Street"] = $row["Street"];
$this->member_array["City"] = $row["City"];
$this->member_array["State"] = $row["State"];
$this->member_array["Country"] = $row["Country"];
$this->member_array["PostCode"] = $row["PostCode"];
$this->member_array["JoinDate"] = $row["JoinDate"];
}
}
/**
* Used to set new member id when you want to change the user
* 1. pass the value of the member id and it would be set as the current member id of the object
* @member_id: The member id of an user
* @return value: Returns TRUE or FALSE depends on whether the member id has been changed for the current object
*/
function set_member_id($member_id){
if (is_integer($member_id)){
$this->member_array["member_id"] = $member_id;
$this->set_member_variables();
return TRUE;
}
}
/**
* Used to store a set of field values into the Members_tbl
* 1. pass the values to be stored as associative arrays should be in the format: field_name ==> field_value
* 2. number of elements in the array is counted and looped so many times to extract each field and its value
* @store_array: the array which holds associative elements
* @return value: returns TRUE or FALSE depends on success of updation of values
*/
function update_values($store_array){
$keys_array = "";
$values_array = "";
$index = 0;
//store the field names in an array
foreach($store_array as $field){
$keys_array[$index++] = "`{$field["name"]}`";
}
//store the fields values in an array
foreach($store_array as $field){
if ($field["type"] == "integer"){
$values_array[$index++] = $field["value"];
}else{
$values_array[$index++] = "'{$field["value"]}'";
}
}
$keys_string = implode(",", $keys_array);
$values_string = implode(",", $values_array);
//form the query
$query = "update `Members_tbl` set ";
//append the set values to the query
for ($i = 0; $i < count($store_array); $i++){
if ($i == count($store_array) - 1){
$query .= "{$keys_array[$i]} = {$values_array[$i]} ";
}else{
$query .= "{$keys_array[$i]} = {$values_array[$i]}, ";
}
}
//append the where condition to the SQL query
$query .= " where `MemberId` = $this->member_id";
echo $query;
//execute the query
if($this->db_connection->updateRows()){
return TRUE
}else{
return FALSE;
}
}
/**
* 1. Used to selected set of fields from the Members_tbl
* 2. Pass the member id and set of fields to be retrieved as an array
* @fields: pass the set of fields to be retrieved as elements of the $fields array
* @return value: an associative array which holds elements having field names and values as keys and values pairs
*/
function get_selected_values($fields){
$return_values = array();
foreach($fields as $key){
$return_values[$key] = $this->member_array[$key];
}
##DEBUG##echo "<br />"; print_r($return_values);
return $return_values;
}
}MemberRemoval Class
Code: Select all
<?php
class MemberRemoval{
/**
* Used to delete a member from the Members_tbl from the given member_id
* 1. Receive the member id and form the delete query to delete the member who has this member id
* @member_id : The unique id of the member
*/
function remove_member($member_id, $db_connection){
$query = "delete from `Members_tbl` where `MemberId` = $member_id";
echo "<br />".$query;
if ($db_connection->updateRows($query)){
return TRUE;
}else{
return FALSE;
}
}
}
?>