Q:

OOP - Sessions and PHP

iam a litte bit confused, i've started to learn php 5 the OO Way last year.  First Projects are done with the Zend Framework. 
But some of my friends are talking about storing Objects in the $_SESSION Superglobal Array.  But iam not able to find an good example, why or when this is nesecarry ? 
It would be great , if someone people can give me a hint , why to store objects in sessions. 

A:

Session is essentially a variable you can store whatever you want in them, A Number, Character, Characters(String), Structs ..... Objects.  
It is also a variable that remains constant through the pages.  Its most commanly used to keep user logged information
How i use Session objects is as follows: 
Lets say i have a users table in my DB and i need to display the firstname and lastname mostly all of my pages. 
What i can do are two things... The Old way:
$result = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE userId=1"));
$_SESSION['userId'] = $result['userId'];
$_SESSION['firstname'] = $result['firstname'];
$_SESSION['lastname'] = $result['lastname'];

Now you can do this which is fine but it would be alot better if you had lets say the following code:
class user{
    _construct($userId){

       $qry_str = mysql_query("SELECT * FROM users WHERE Id=$userId");

       $result = mysql_fetch_array($qry_str);
       $this->userId = $userId;
       $this->firstname = $result['firstname'];
       $this->lastname = $result['lastname'];
    }

    public $userId, $firstname, $lastname;
}

And to initialize
$_SESSION['user'] = new user(1);

Or say Multiple users:
$_SESSION['user'][1] = new user(1);
$_SESSION['user'][2] = new user(2);
$_SESSION['user'][3] = new user(3);

And then in your code all you need to do is: assign the user by userId at login and then use the class that is stored to display all your information. 
There are alot more powerfull things you can do with classes.  But above shows you a simple and effective solution. 
Try this: Add if statments and die statments in your construct for the user class... that when constructing the user you can error check
Hope this Helps

