473,225 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

when to serialize/unserialize


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
6 1920
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Smith | last post by:
Hi, I am using a custom Session Handler. session_set_save_handler is working well. But i want to read the data direct from the database. My problem: php don't uses the standard serialize...
2
by: Andrew | last post by:
Some have suggested that using serialize() and unserialize is faster than reading/writing an array to disk as a simple text file using $array = file('numbers.txt'); Can anyone justify this? ...
7
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
2
by: onefastmustang | last post by:
I have a cookie that I serialize and set as follows.. $searchdata = $state; $searchdata = $country; $searchdata = $radius; $searchdata = $radius_zip; if ($_GET){ foreach ($_GET as $value){...
1
by: Mark | last post by:
I'd like to take an instance of a class, and serialize it to a file on my computer. No security required on the file. Then, I'd like to be able to unserialize the file later and use the instance...
2
by: Derek Martin | last post by:
Hey list, I have an arraylist containing some objects that I want to serialize and send over the internet and then deserialize back into the arraylist of objects. What I have so far: Dim...
4
by: jody.florian | last post by:
Hi there, http://uk2.php.net/setcookie suggests that for security, implode (correction of explode) should be used instead of serialize. Does anyone know why? I don't need to implement this,...
6
by: dawnerd | last post by:
Hello everyone. I have a question, or problem if you will, that I'm sure someone knows the answer to. I have a database that stores information on a given user. The information is stored in a...
1
by: VooDoo | last post by:
Hi, I am using the serialize and unserialize to put and get data from mysql database. I think i could optimise the way i handle the data, but not really sure how. What is the best way to happend...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.