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

Sessions and Objects

P: n/a
Hi,

I'm currently building an object-oriented shopping cart with PHP 4.3.2 and
MySQL 4.0.14.

I am looking at storing a Cart object within a PHP session, however I am not
completely sure on the best way to achieve this. I have read up and there
seems to be varying advice. Should I need to serialise and unserialise the
object or is this automatic and should I be using session_register()? For
reference, register_globals is off.

I'd be grateful if anybody could help.

Thanks in advance,
Matt
Jul 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Matthew Bates" <ma********@hotmail.com> wrote in message
news:bz*********************@news-text.cableinet.net...
Hi,

I'm currently building an object-oriented shopping cart with PHP 4.3.2 and MySQL 4.0.14.

I am looking at storing a Cart object within a PHP session, however I am not completely sure on the best way to achieve this. I have read up and there seems to be varying advice. Should I need to serialise and unserialise the object or is this automatic and should I be using session_register()? For reference, register_globals is off.

I'd be grateful if anybody could help.


With your version of PHP, you wouldn't want to use session_register()
since it's deprecated.

This also means, your objects are not (un)serialised automatically. So
you'd want to do something like:

At the start of the page:
- include the class (since serialized objects do not contain the class
functions)
- start the session (this must be after the class include)
- unserialize the object

At the end of the page:
- serialize the object

For more info http://php.net/manual/en/language.oop.serialization.php

Jul 16 '05 #2

P: n/a
On Tue, 2 Sep 2003 02:04:43 +0800, "powerboy"
<po******@rarextreme.com> wrote:
"Matthew Bates" <ma********@hotmail.com> wrote:
I'm currently building an object-oriented shopping cart with PHP4.3.2 and
MySQL 4.0.14. Should I need to serialise and unserialise the
object or is this automatic and should I be using
session_register()? For
reference, register_globals is off.

This also means, your objects are not (un)serialised automatically. At the start of the page:
- include the class (since serialized objects do not contain the class
functions)
- start the session (this must be after the class include)
- unserialize the object

At the end of the page:
- serialize the object

For more info http://php.net/manual/en/language.oop.serialization.php


my experience agrees with what the first comment on that page is:
objects are automatically (un)serialized when putting in and getting
out of a session.

if you have resources in your object (like a database connection), you
may want to look at __sleep and __wakeup to shut down and restore them
properly.
Jul 16 '05 #3

P: n/a
Matthew Bates schrieb:
Hi,

I'm currently building an object-oriented shopping cart with PHP 4.3.2 and
MySQL 4.0.14.

I am looking at storing a Cart object within a PHP session, however I am not
completely sure on the best way to achieve this. I have read up and there
seems to be varying advice. Should I need to serialise and unserialise the
object or is this automatic and should I be using session_register()? For
reference, register_globals is off.

I'd be grateful if anybody could help.

Thanks in advance,
Matt


With your PHP version, storing objects in the session should work quite
easy - just set
$_SESSION["cart"] = $your_card_object;

i.e., no serizalize/unserialize is necessary.

One problem may occure when you will retrive the object from the session
- you need to include the class definition before the session starts!

But I found it easier to store only object IDs in the session and to
store/retrieve the object itself from the database.

Regards!
Rainer

--
------------------------------------------------
Rainer Herbst Linux - Registered
ZEIK User #319157
Universitšt Potsdam Usual disclaimers applies!
------------------------------------------------

Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.