Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/390135797351186432
deleted 6 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

PHP classes, user User system using PDO

I'm making a very basic user system, and this is what I have so far:

Then on my main script, I include the user.class.php user.class.php (as shown above) and do this:

Is this all correct? Or have I misunderstood? Is there a way of simplifying this? I've tried to keep the code as simple as possible. Am I correct for having getters (getEmailgetEmail, etc.) as well as methods such as canViewPagecanViewPage, within the user class?

Also, should it be:

        $userGroup = new UserGroup($this->userGroup); 

or should I do something like this again:?

(is Is there a simpler way?)

Cheers

PHP classes, user system using PDO

I'm making a very basic user system and this is what I have so far

Then on my main script, I include the user.class.php (as shown above) and do this:

Is this all correct? Or have I misunderstood? Is there a way of simplifying this? I've tried to keep the code as simple as possible. Am I correct for having getters (getEmail etc) as well as methods such as canViewPage, within the user class?

Also should it be:

        $userGroup = new UserGroup($this->userGroup); 

or should I do something like this again:

(is there a simpler way?)

Cheers

User system using PDO

I'm making a very basic user system, and this is what I have so far:

Then on my main script, I include the user.class.php (as shown above) and do this:

Is this all correct? Or have I misunderstood? Is there a way of simplifying this? I've tried to keep the code as simple as possible. Am I correct for having getters (getEmail, etc.) as well as methods such as canViewPage, within the user class?

Also, should it be:

$userGroup = new UserGroup($this->userGroup); 

or should I do something like this again?

Is there a simpler way?

Source Link
Tom
  • 121
  • 2

PHP classes, user system using PDO

I'm making a very basic user system and this is what I have so far

<?php


/* The class for constructing any user's information
 */

    class User {
        
        protected $userId, $email, $userGroup;
        
        protected function getEmail() {
            return $this->email;
        }
        
        protected function getUserId() {
            return $this->userId;
        }
        

        public function __construct($userId, $email, $emailGroup) {
            $this->userId = $userId;
            $this->email = $email;
            $this->userGroup = $emailGroup;
        }

        // Method for checking whether user can view a specific page
        public function canViewPage($pageId) {
            $userGroup = new UserGroup($this->userGroup);
            if($userGroup->canViewPage($pageId)) {
                return true;
            }else{
                return false;
            }       
        }
    
        // Method for checking permissions
        public function hasPermissions($alias) {
            /* alias is the name of the permissions. ie can-book
            * create usergroup object based on current user's usergroup */
            
            $userGroup = new UserGroup($this->userGroup); 
            if($userGroup->hasPermissions($alias)) {
                return true; 
            }else{
                return false;
            }
        }

    }
    
    class UserMapper {
        // database connection
        private $db;
        
        public function __construct($db)
        {
            $this->db = $db;
        }
        
        public function findByUserId ($userId) {
            $userObject = new User();
            $q = $this->db->prepare("SELECT userId, email, userGroup FROM user WHERE userId = :userId");
            $q->bindValue(":userId", $id);
            $q->setFetchMode( PDO::FETCH_INTO, $userObject);
            $q->execute();
            $q->fetch(PDO::FETCH_INTO);
            return $userObject;
            
        }
    }   
?>

Then on my main script, I include the user.class.php (as shown above) and do this:

<?php 
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::ATTR_PERSISTENT => true));
    $getUser = new UserMapper($dbh);
    $user = $getUser->findByUserId(41);
    echo $user->getEmail();
?>

Is this all correct? Or have I misunderstood? Is there a way of simplifying this? I've tried to keep the code as simple as possible. Am I correct for having getters (getEmail etc) as well as methods such as canViewPage, within the user class?

Also should it be:

        $userGroup = new UserGroup($this->userGroup); 

or should I do something like this again:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::ATTR_PERSISTENT => true));
        $getUserGroup = new UserGroupMapper($dbh);
        $userGroup = $getUserGroupMapper->findByUserId($this->userGroup);

(is there a simpler way?)

Cheers