473,396 Members | 1,805 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,396 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 1929
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.