when to serialize/unserialize

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

    #1

    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_registe r('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($th at)
    ....when the system seems to be doing it anyway.

    }

    --
    /* Sandy Pittendrigh >--oO0>
    ** http://montana-riverboats.com
    */
  • Andy Hassall

    #2
    Re: when to serialize/unserialize

    On Mon, 12 Sep 2005 07:19:56 -0700, sandy <salmobytes@slo wtorture.spamme rs.com>
    wrote:
    [color=blue]
    >So why and when
    >do you ever need to explicitly say something
    >about serialize($this ) or unserialize($th at)
    >...when the system seems to be doing it anyway.[/color]

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

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Colin McKinnon

      #3
      Re: when to serialize/unserialize

      sandy wrote:
      [color=blue]
      >
      > So, I've been trying to learn how php sessions work.
      > I'm new at php sessions. Bear with me.
      >[/color]
      <snip>[color=blue]
      > session_start() ;
      > $test_class = new TestClass();
      > session_registe r('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($th at)
      > ...when the system seems to be doing it anyway.
      >
      > }
      >[/color]

      You're writing your code assuming that register_global s 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.

      Comment

      • BKDotCom

        #4
        Re: when to serialize/unserialize

        don't use session_registe r (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

        Comment

        • Chung Leong

          #5
          Re: when to serialize/unserialize

          If you use session.auto_st art, then you need to serialize objects
          manually, because when PHP unserialize the session, your classes
          wouldn't have been declared yet.

          Comment

          • Oli Filth

            #6
            Re: when to serialize/unserialize

            BKDotCom said the following on 12/09/2005 17:15:[color=blue]
            > don't use session_registe r (pretend that function doesn't exist)
            > just use the superglobal $SESSION array
            >[/color]

            Make that $_SESSION.

            !


            --
            Oli

            Comment

            • sandy

              #7
              Re: when to serialize/unserialize

              [color=blue]
              > You're writing your code assuming that register_global s is on - not a good
              > idea, particularly when you're learning.[/color]

              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_GLOBAL S, 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
              */

              Comment

              Working...