8

Comments on a session storage class design

 2 years ago
source link: https://www.codesd.com/item/comments-on-a-session-storage-class-design.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Comments on a session storage class design

advertisements

I have a session class that basicly just sets and retrieves session variables, the reason I made it was so I could easily change it to use sessions or something like memcache to set the items and have them accessible on multiple pages without hitting the database

I then have this user class which uses the session object to get session variables in it.
I am wanting to add to this user class though, to make it more encapsulated I would like to be able to set the variables that I am retrieving in this class so right now I can display the userid with $user->userid; I would like to first have a method or something that sets its value from the session object I guess Does this sound lke a good idea or possibly a lot of overhead?

And if what I am trying to do is a good idea maybe you could suggest/show example of how I should do it? I am thinking that if I add that method in that possibly I should move the code in the __construct method into it's own method

Basicly, I have the variables listed in the top part of the class that are used in the construct method, if I have multiple methods in the class though would I need to set them all at the top like that?

<?PHP
//user.class.php file
class User
{
    public $userid;
    public $name;
    public $pic_url;
    public $gender;
    public $user_role;
    public $location_lat;
    public $location_long;
    public $newuser;

    function __construct()
    {
        global $session;
        if($session->get('auto_id') != ''){
            //set user vars on every page load
            $this->userid = $session->get('auto_id'); //user id number
            $this->name = $session->get('disp_name');
            $this->pic_url = $session->get('pic_url');
            $this->gender = $session->get('gender');
            $this->user_role = $session->get('user_role');
            $this->location_lat = $session->get('lat');
            $this->location_long = $session->get('long');
            $this->newuser = $session->get('newregister');
        }else{
            return false;
        }
    }
}

//with the class above I can easily show some user variables I have saved into a session like this below
$user = new user();
$user->userid;

?>


In general your idea is a good one 3 things I would do differently:

1) In your implementation doesn't seem to consider having several users. ie Several instances of the same class.

2) I would use factories instead of using IF in the constructor. So for a user you have saved in the session you would call:

 $savedUser = User::fromSession($userId);

for a new user

  $user = new User()

3) Use the serialize and unserialze functions to save that data to the session

Then your class could could be implemented as

public static function fromSession($userId) {
   return unserialize($session->get('users_'.$userId));
}

public function save() {
   return $session->set('users_'.$this->id , serialize($this));
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK