OOP approach for user registration

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
topcatxx
Forum Newbie
Posts: 1
Joined: Wed May 13, 2009 6:46 am

OOP approach for user registration

Post by topcatxx »

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:

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;
    }
}
and then call it as follows:

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";
}
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.
Last edited by Benjamin on Wed May 13, 2009 12:55 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP approach for user registration

Post by Christopher »

I think you are headed in generally the right direction. It makes sense to have everything that can be done to a user encapsulated in a single class -- so insert, update or delete.
(#10850)
Post Reply