session/class interaction

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

session/class interaction

Post by Luke »

If I have a class set up and I want my session's array values to be identical to a certain class's properties, what is the best way to set those?
Here is what i have done:

Code: Select all

$auth = new authorize($dbh);
$auth->login("Luke", "pass");
$_SESSION['user_name'] = $auth->user_name;
$_SESSION['pass_word'] = $auth->pass_word;
$_SESSION['user_level'] = $auth->user_level;
There are quite a few more properties than that, but you get the picture.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Why not just store the object in the session:

Code: Select all

$_SESSION['auth'] =& $auth;
The only trick you need to do is make sure that the Auth class is always loaded before session_start() is called (or use autoload)
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK, thanks!
Post Reply