when to serialize/unserialize

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

With java servlets I can declare complex object-oriented class
structures as session variables in a servlet.
That means I can have a complex HTML form that submits
iteratively back to the server for interim updates, before
a final save (interim post events allow users to add values to dropdown  
menus, collapse parts of the form, etc). And it all works
because Java so seamlessly takes care of serializing a complex
class structure for me--you never have to explicitly say
anything about serialize/unserialize in Java servlet programming.
The system does it for you.

So, I've been trying to learn how php sessions work.
I'm new at php sessions. Bear with me.

I defined a simple helloworld test class definition,
that includes a hashed array of test values.
In a test.php, if $REQUEST_METHOD=='GET'
I instantiate an instance of the class, and then
print out a form whose method=='POST'
...inside the GET block I also have
$test_class = new TestClass();

I can say:
...and it works. So php must have automatically
serialized the class for me. So why and when
do you ever need to explicitly say something
about serialize($this) or unserialize($that)
...when the system seems to be doing it anyway.


/* Sandy Pittendrigh   >--oO0>
** http://montana-riverboats.com

Re: when to serialize/unserialize


Quoted text here. Click to load it

 When you want to save it somewhere else that doesn't have automatic
serialisation, such as to a file.

Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

Re: when to serialize/unserialize

sandy wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

You're writing your code assuming that register_globals is on - not a good
idea, particularly when you're learning.

In answer to your question - 'cos sometimes you may want to keep an object
somehwere other than the session.


Re: when to serialize/unserialize

Quoted text here. Click to load it

Thanks. I hadn't even noticed that (that I was using a global variable).

I'm doing this on a shared host box. I asked them to turn off  
REGISTER_GLOBALS, and they did.

$_SESSION['whatever'] works just fine.

It never occured to me I could serialize a class instance, at a
moment in time, as if it were a function derivative.

I always parse the data structure and then punch values into the  
database, or versa visa from the database to the class instance.

Thank you.

/* Sandy Pittendrigh   >--oO0>
** http://montana-riverboats.com

Re: when to serialize/unserialize

don't use session_register  (pretend that function doesn't exist)
just use the superglobal $SESSION array

$SESSION['text_class'] = new TestClass();

as others have said, you would use serialize/unserialize when saving to
a file/DB/cookie, etc

Re: when to serialize/unserialize

BKDotCom said the following on 12/09/2005 17:15:
Quoted text here. Click to load it

Make that $_SESSION.



Re: when to serialize/unserialize

If you use session.auto_start, then you need to serialize objects
manually, because when PHP unserialize the session, your classes
wouldn't have been declared yet.

Site Timeline