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.