We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23491 ☆ A M B ☆
    • 1,056 Posts
    I have a defined PHP class used for creating an object, and I am trying to store in $_SESSION scope using MODx.

    I am working with $_SESSION in MODx currently without issues (Using Plugin/System events); however, if I specifically try storing a defined object in $_SESSION scope, I am getting errors on page-reload regarding the object/class definition needing to be loaded before anything else (references to building an __autoload function, etc )

    I have tried, to my knowledge, to get the "serialization" (as I think it’s called) to occur properly to enable stored of PHP Objects in session, all without luck huh

    Anyone have any experience in the matter, or some advise to getting me on the right path? (Using PHP 5+)
      Mike Reid - www.pixelchutes.com
      MODx Ambassador / Contributor
      [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
      ________________________________
      Where every pixel matters.
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: pixelchutes at Jan 18, 2007, 10:04 AM

      I have a defined PHP class used for creating an object, and I am trying to store in $_SESSION scope using MODx.

      I am working with $_SESSION in MODx currently without issues (Using Plugin/System events); however, if I specifically try storing a defined object in $_SESSION scope, I am getting errors on page-reload regarding the object/class definition needing to be loaded before anything else (references to building an __autoload function, etc )

      I have tried, to my knowledge, to get the "serialization" (as I think it’s called) to occur properly to enable stored of PHP Objects in session, all without luck huh

      Anyone have any experience in the matter, or some advise to getting me on the right path? (Using PHP 5+)
      Right, you can’t store objects in session that have any kind of internal PHP resource (i.e. db connection, file pointer, etc.) without using the magic __autoload and/or __wakeup functions to restore those things approriately. Other than that, I also do not recommend storing such things in a session; object serialization in PHP sucks, slowly...
        • 23491 ☆ A M B ☆
        • 1,056 Posts
        Hmmm... what would you recommend for an alternative, a built-out associative array, db storage, etc?

        Will "future" versions of MODx make accomplishing tasks like this easier? (Although I feel techniques discussed here are not necessarily specific to MODx)
          Mike Reid - www.pixelchutes.com
          MODx Ambassador / Contributor
          [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
          ________________________________
          Where every pixel matters.
          • 33372
          • 1,611 Posts
          You may have better luck storing the component values of the object in the session as a simple associative array and then reassembling them as an object when you need to use it.

          EDIT: Looks like you already had the same idea...

          As far as I know, the problem is in PHP’s serialize and unserialize functions, which can cause major headaches and you are better off avoiding.
            "Things are not what they appear to be; nor are they otherwise." - Buddha

            "Well, gee, Buddha - that wasn't very helpful..." - ZAP

            Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
            • 23491 ☆ A M B ☆
            • 1,056 Posts
            Ah, I believe I have seen this done before...does it look something like:

            {a:2, b:1, c:{ d:"hello" }, ... } 
            


            Any ideas/references on being able to pass an object to create the component values/reassemble? I’m sure I could code it myself, but if there’s an existing/tested version somewhere, I wouldn’t mind testing the idea!
              Mike Reid - www.pixelchutes.com
              MODx Ambassador / Contributor
              [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
              ________________________________
              Where every pixel matters.
              • 33372
              • 1,611 Posts
              There are some ideas and methods here:
              http://www.php.net/manual/en/language.oop.serialization.php
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 23491 ☆ A M B ☆
                • 1,056 Posts
                Perfect! Thank you for the suggestion wink
                  Mike Reid - www.pixelchutes.com
                  MODx Ambassador / Contributor
                  [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                  ________________________________
                  Where every pixel matters.
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Use xPDO. wink

                  It has built in caching of objects (in PHP executable format; instead of serializing it writes the code to a file or other storage location from where it can be included/eval’d) to file/memory/wherever, and handles all of the complications for you...it even has built-in JSON serialization for speeding up AJAX requests, and storing just the persistent properties of an object is as easy as saying
                  $_SESSION[$somekey]= $object->toArray();
                  and restoring from session just as easy, by using
                  $object= $xpdo->newObject('MyClassName');
                  $object->fromArray($_SESSION[$somekey]);


                  Or borrow the techniques from the xPDO caching layer for extracting appropriate object vars from your objects; which uses PHP’s get_object_vars() function.
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Just FYI, I opened a subforum specifically for discussing xPDO and split this topic after this point to http://modxcms.com/forums/index.php/topic,11236.0.html