Sessions and Objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Bates

    Sessions and Objects

    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_registe r()? For
    reference, register_global s is off.

    I'd be grateful if anybody could help.

    Thanks in advance,
    Matt


  • powerboy

    #2
    Re: Sessions and Objects

    "Matthew Bates" <mattybates@hot mail.com> wrote in message
    news:bzL4b.2382 $1K1.20652303@n ews-text.cableinet. net...[color=blue]
    > Hi,
    >
    > I'm currently building an object-oriented shopping cart with PHP[/color]
    4.3.2 and[color=blue]
    > MySQL 4.0.14.
    >
    > I am looking at storing a Cart object within a PHP session, however[/color]
    I am not[color=blue]
    > completely sure on the best way to achieve this. I have read up and[/color]
    there[color=blue]
    > seems to be varying advice. Should I need to serialise and[/color]
    unserialise the[color=blue]
    > object or is this automatic and should I be using[/color]
    session_registe r()? For[color=blue]
    > reference, register_global s is off.
    >
    > I'd be grateful if anybody could help.[/color]

    With your version of PHP, you wouldn't want to use session_registe r()
    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



    Comment

    • Gerhard Fiedler

      #3
      Re: Sessions and Objects

      On Tue, 2 Sep 2003 02:04:43 +0800, "powerboy"
      <powerboy@rarex treme.com> wrote:
      [color=blue]
      >"Matthew Bates" <mattybates@hot mail.com> wrote:[color=green]
      >> I'm currently building an object-oriented shopping cart with PHP[/color]
      >4.3.2 and[color=green]
      >> MySQL 4.0.14.[/color][/color]
      [color=blue][color=green]
      >> Should I need to serialise and unserialise the
      >> object or is this automatic and should I be using
      >> session_registe r()? For
      >> reference, register_global s is off.[/color][/color]
      [color=blue]
      >This also means, your objects are not (un)serialised automatically.[/color]
      [color=blue]
      >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[/color]

      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.

      Comment

      • Rainer Herbst

        #4
        Re: Sessions and Objects

        Matthew Bates schrieb:[color=blue]
        > 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_registe r()? For
        > reference, register_global s is off.
        >
        > I'd be grateful if anybody could help.
        >
        > Thanks in advance,
        > Matt
        >
        >[/color]

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

        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!
        ------------------------------------------------

        Comment

        Working...