I am a beginner to OOP, but I have been using php/mysql for a while.

I understand how the oop concepts work, but not sure how it would integrate with sessions.

for example, if a user login to my website on page 1, i can pass his variables to page 2 using sessions. But how would it work if i had used an object instead of a bunch of variables?

Can I pass an object via session? Or do I have to initialize the object, update the database, pass a key variable, then on page 2, use the key variable to retrieve the object from the db? Maybe I am a little confused but that doesnt seem at all efficient.

Can anyone explain?

Thanks.

    You certainly can pass an object. Just use
    $SESSION['object'] =& $object;
    when you've created the object, then
    $object =& $
    SESSION['object'];
    in later pages.

    If you have references in your objects, they will be de-referenced when they go into the session. This is bad if you have (for example) any circular references, because you will end up with multiple copies of the object instead of multiple references to the same object. This can be avoided by using [man]serialize[/man] and [man]unserialize[/man] instead of putting the object into the session directly.

      thanks swr.

      So is =& kinda like assigning the address of the object?

      Is that what I am passing to page 2?

        It causes the two variables to share the same "address"; it doesn't assign the address itself. The result is that changes to $object also affect $_SESSION['object']. See the [man]references[/man] manual page for details.

        When assigning objects in PHP you almost always want to use the &. That way you reference the same instance, instead of copies. If you don't pass objects by reference you may find that you pass an object, it gets copied, the function manipulates the copy, then discards the copy, leaving you wondering why the original object that you tried to pass was not changed.

          Write a Reply...