Reputation: 393
I have user information which I wants to retain in session. I am using memcache as session save handler. I have two approach for storing user info
$_SESSION['uname'] = 'abx';
$_SESSION['email'] = '[email protected]';
$_SESSION['profilepage'] = 'http://t';
$_SESSION['role'] = '1';
$_SESSION['profilepicture'] = 'http://t';
$_SESSION['gender'] = 'M';
OR
$obj = new stdClass();
$obj->uname = 'abx';
$obj->email = '[email protected]';
$obj->profilepage = 'http://t';
$obj->role = 1;
$obj->profilepicture = 'http://t';
$obj->gender = 'M';
$_SESSION['user'] = $obj;
I wants to know which one approach is better. Do having multiple session variables has any performance impact.
Upvotes: 2
Views: 1840
Reputation: 2837
In addition to st2erw2od's answer (I absolutely agree with him), I only want to add that to me this does not sound like a good use-case for a session.
The session should not contain all of the user's data and / or complete objects with userdata. It should only contain the necessary data, e.g. which user is authenticated. Every other stuff like his e-mail address, profile picture, etc. should then be loaded from the database (file system, etc.) according to specific rules (e.g. when they're needed or a complete user entity, etc.). So in this case I recommend just saving the user ID in the session and then load the information from the database.
The main advantage of this is: a) Clearer (and less code); b) Your information is always up-to-date and synced with the database, otherwise you could have some severe bugs.
Upvotes: 2
Reputation: 725
The performance impact with just this little data amount you mentioned in the question and comments is very little to non-existent. Internally you will be able to measure some very small differences with a benchmark test, but this won't speed things up more.
In this scenario I would try and consider the benefits of clean, reusable and maintainable code over performance. The second object-oriented approached makes a lot more sense in my opinion and is considered best-practice.
Upvotes: 4