Link to home
Start Free TrialLog in
Avatar of pret32
pret32

asked on

Objects stored in PHP session?

Hi all,

Is it possible to store a complex object in the session?

class {

var $arra; //10 elements
var
....

}

it doesn't work by me. what could be a workaround?
I can only store simple variables in the session objects, arrays also doesn't work....

Regards,
pret
Avatar of VGR
VGR

yes

and arrays DO WORK

I use them all the time 8-)
it does work in fact on most ecommerce applications i have made i use this to store the contents of the shopping cart.

$cart = new Cart();

session_register("cart");

$_SESSION['cart']->AddItem();


etc... arrays work the same way

$array = array();

session_register("array");

$_SESSION['array']['0'];
$_SESSION['array']['1'];

etc etc...

Storing (serializing) objects in session should work fine, but you need to always include the class definition before trying to do anything with the serialized object.  Only the object variables are saved in the session.

Also, the default behavior for PHP is to save the entire session variable to file after each page hit, usually in the /tmp directory.  So, if you are going to be storing an Object in your session, it's a good idea to try to keep it small.  The best way to do this is just reduce the number of object variables, and don't store huge arrays in your object.
Avatar of pret32

ASKER

When i stored a class object that contained array and other variables, seem to work (no error messages), but when i said $var=$_SESSION['class object'] and then $var->method() i get the following error message:

Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition cheider of the object you are trying to operate on was loaded _before_ the session was started in bspern.php on line 26


what have i overseen?
Avatar of pret32

ASKER

you mean that when i am only doing $var=$_SESSION['class object'] and then $var->method() in script i should make include('classdefinition.php'); before the line of $var...?
the object should be completely 'filled in" before being registered via $_SESSION['name']=$refObject;
Avatar of pret32

ASKER

it is completely filled before $_SESSION['var']=$var;

but what about this error message?
Have you included the class BEFORE you start the session on the new page?

You have to tell php what it is before you reload it into memory, otherwise it doesn't know how to handle it.
Yes.

Do this...

when your done using it/filling it on a page...
// save code
$_SESSION["var"]  = serialize($var);


When you want to retrieve it for use on another page.
// retrieve code
$temp = $_SESSION["var"];
$var = unserialize($temp);



This breaks down complex structured data types into a basic linear/contiguous data type.

You can also do this to store object data in session variables...


Hope this helps...
Avatar of pret32

ASKER

I added with require function the class definition before i read the class object from the session and he says

Fatal error: Call to undefined function: getname()

what's wrong?
let us see your code...
Your not going to have a valid object unless your serializing and unserializing it/ while storing it as a session...  That would give you that error...

Check out this article...

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=809&lngWId=8
Avatar of pret32

ASKER

Before session_start i include the class definitions and when i read a complex object from the session and then access the method of an object that is stored in the complex object and i get the error message that i am calling the undefined function. What's wrong?
I cannot solve my problem....
Are my comments not showing up?  
Avatar of pret32

ASKER

yeah, but when i simply serialize and unserialize it it doesn't make any difference!
Avatar of pret32

ASKER

if the complex object ist read and then by access method to delegated object comes the problem and class definitions are added before session_start what else could be wrong.....
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial