OOP approach for user registration
Posted: Wed May 13, 2009 7:14 am
Hi,
Hope I've posted this in the right place! I'm trying to create a backend for a website and want to do it in OOP to get to grips with it. I'm having problems deciding what responsibilities each class should have and the best way to go about it:
for example I have created a user class that registers new users with the code as follows:
and then call it as follows:
Is it common to have seperate objects for adding a user, editing a user etc as I would need to do this with the approach I have taken? I'm trying to figure out if I'm trying to get my class to do too much and whether I should split the logic up - but it seems I'm just getting closer to procedural code with functions wrspped in classes? I've seen loads of examples of certain principles of oop such as inheritance etc but no decent examples of creating a complete system using combinations of these theories - anyone know a site with good examples eg creating a simple cms or something similar?
thanks in advance.
Hope I've posted this in the right place! I'm trying to create a backend for a website and want to do it in OOP to get to grips with it. I'm having problems deciding what responsibilities each class should have and the best way to go about it:
for example I have created a user class that registers new users with the code as follows:
Code: Select all
class User{
private $dbConn;
private $formData;
private $query;
private $isEmailUnique;
private $table;
/*
* set up properties
*/
public function __construct($formData){
$this->dbConn = connectionManager::dbConn();
$this->isEmailUnique = false;
$this->table = "users";
/***
Clean the data and store it into an array
***/
$this->formData['title'] = dataCleaner::sterilise($formData['title'],"text");
$this->formData['first_name'] = dataCleaner::sterilise($formData['first_name'],"text");
$this->formData['last_name'] = dataCleaner::sterilise($formData['last_name'],"text");
$this->formData['tel'] = dataCleaner::sterilise($formData['tel'],"int");
$this->formData['email'] = dataCleaner::sterilise($formData['email'],"text");
$this->formData['password'] = dataCleaner::sterilise($formData['password'],"text");
$this->formData['cv'] = dataCleaner::sterilise($formData['cv'],"text");
$this->formData['date_joined'] = dataCleaner::sterilise($formData['date_joined'],"text");
}
/*
* the email should be unique if so set the emailUnique property to true otherwise set it to false and ask for a new one.
*/
public function isUnique(){
$uChecker = new checkUnique($this->table,"email",$this->formData['email'],$this->dbConn);
$this->isEmailUnique = $uChecker->test();
return $this->isEmailUnique;
}
public function insertUser(){
if($this->isEmailUnique==false)echo "not done the test yet";
$qB= new queryBuilder($this->table, $this->formData);
$this->query =$qB->insertRecord();
execQuery($this->query);
sendConfEmail();
addUserToForum();
}
private function addUsertoForum(){
//not written yet
}
private function execQuery(){
try{
$this->dbConn->query($this->query);
}catch(PDOException $e){
/* for debugging only */
#echo $e->getMessage();
exit;
}
}
public function getQuery(){
return $this->query;
}
}Code: Select all
$test = new User($_POST);
if($test->isUserUnique()){
$test->insertUser();
echo($test->getQuery());
}else{
echo "Sorry this email address is already registered with us";
}thanks in advance.