By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

when to serialize/unserialize

P: n/a

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
session_start();
$test_class = new TestClass();
session_register('test_class')
So, if $REQUEST_METHOD=='POST'
{
I can say:
$test_class->show();
....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
*/
Sep 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mon, 12 Sep 2005 07:19:56 -0700, sandy <sa********@slowtorture.spammers.com>
wrote:
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.


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

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 12 '05 #2

P: n/a
sandy wrote:

So, I've been trying to learn how php sessions work.
I'm new at php sessions. Bear with me.
<snip> session_start();
$test_class = new TestClass();
session_register('test_class')
So, if $REQUEST_METHOD=='POST'
{
I can say:
$test_class->show();
...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.

}


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.

C.
Sep 12 '05 #3

P: n/a
don't use session_register (pretend that function doesn't exist)
just use the superglobal $SESSION array

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

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

Sep 12 '05 #4

P: n/a
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.

Sep 12 '05 #5

P: n/a
BKDotCom said the following on 12/09/2005 17:15:
don't use session_register (pretend that function doesn't exist)
just use the superglobal $SESSION array


Make that $_SESSION.

!
--
Oli
Sep 12 '05 #6

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


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
*/
Sep 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.