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

&lt;?php


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

    class User {

        protected $userId, $email, $userGroup;

        protected function getEmail() {
            return $this-&gt;email;
        }

        protected function getUserId() {
            return $this-&gt;userId;
        }


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

        // Method for checking whether user can view a specific page
        public function canViewPage($pageId) {
            $userGroup = new UserGroup($this-&gt;userGroup);
            if($userGroup-&gt;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-&gt;userGroup); 
            if($userGroup-&gt;hasPermissions($alias)) {
                return true; 
            }else{
                return false;
            }
        }

    }

    class UserMapper {
        // database connection
        private $db;

        public function __construct($db)
        {
            $this-&gt;db = $db;
        }

        public function findByUserId ($userId) {
            $userObject = new User();
            $q = $this-&gt;db-&gt;prepare("SELECT userId, email, userGroup FROM user WHERE userId = :userId");
            $q-&gt;bindValue(":userId", $id);
            $q-&gt;setFetchMode( PDO::FETCH_INTO, $userObject);
            $q-&gt;execute();
            $q-&gt;fetch(PDO::FETCH_INTO);
            return $userObject;

        }
    }   
?&gt;


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

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


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-&gt;userGroup); 


or should I do something like this again?

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


Is there a simpler way?
I didn't change much.
I think you're correct in having getters as well as methods such as 'canViewPage'.
The Methods 'canViewPage' and 'hasPermissions' are just wrappers around the 'UserGroup' class, the logic is in 'UserGroup'. It makes sense to create wrappers around 'UserGroup', for example if you want to have also Permission based solely on the User(Example a root user) then you would just have to change these Methods. If you had invoked the Methods directly on the 'UserGroup' - not over the 'User' - you would have to go over all these places. Pherhaps it would be Usefull to have a 'Permissions' class, wich would have a constructor or factory method, where you could give an 'User' as Argument.

&lt;?php
/* The class for constructing any user's information
*/

class User {

    protected $userId, $email, $userGroup;

    protected function getEmail() {
        return $this-&gt;email;
    }

    protected function getUserId() {
        return $this-&gt;userId;
    }


    public function __construct($userId, $email, $emailGroup) {
        $this-&gt;userId = $userId;
        $this-&gt;email = $email;
        // You never use userGroup directly, you can just 
        // put the object as membervariable, so you don't 
        // have to instantiate the object twice
        $this-&gt;userGroup =  new UserGroup($emailGroup);
    }

    // Method for checking whether user can view a specific page
    public function canViewPage($pageId) {
        /*
        if(condition){
            return true;
        else
            return false;
        is equals
        return (bool) condition
        */
        return (bool) $this-&gt;userGroup-&gt;canViewPage($pageId);
    }

    // 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 */
        return (bool) $this-&gt;userGroup-&gt;hasPermissions($alias);
    }

}

class UserMapper {
    // database connection
    private $db;

    public function __construct(\PDO $db)
    {
        $this-&gt;db = $db;
    }

    public function findByUserId ($userId) {
        $userObject = new User();
        $q = $this-&gt;db-&gt;prepare("SELECT userId, email, userGroup FROM user WHERE userId = :userId");
        $q-&gt;bindValue(":userId", $id);
        $q-&gt;setFetchMode( PDO::FETCH_INTO, $userObject);
        $q-&gt;execute();
        $q-&gt;fetch(PDO::FETCH_INTO);
        return $userObject;

    }
}   
?&gt;


I don't understand your Second Questions, pherhaps you made some typos?
You have '$getUserGroup', but use '$getUserGroupMapper' and you give the method 'findByUserId' a UserGroup. 

EDIT:
Now I understand. If you have a lot of UserGroups, I think it would be good. If you have just around 5 UserGroups, I thinks it would be too much

Your main program is good, for being an example.