Autoloading class definitions of composed objects on unserialize() from session

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

    #1

    Autoloading class definitions of composed objects on unserialize() from session

    Hello everyone!
    I'm currently working on a MVC-framework and have run into an issue.

    I'm using separate script files to render the active page's CSS and
    Javascript Code.
    I want to pull the data from the page controller object, which is
    initialized in another script.
    I thought this wouldn't be a problem if I simply stored the controller
    in the session-file, but the unserialization of the object simply
    won't work.
    The controller has properties that are of different class types than
    the controller itsself (page-secific Model and View objects).

    My Questions:

    1. Are there any problems using $myObj=$_SESSIO N['myObj']; ?
    I thought that as of PHP5 this should work?

    2. do class definitions of contained objects also have to be known
    when i unserialize?
    (like my Model/View objects)

    3. can I get __autoload to help me out of this?

    4. if not, can i perform some sort of __sleep()/__wakeup() magic to
    achieve the same effect?

    Any help is very much appreciated, thx.

  • ZeldorBlat

    #2
    Re: Autoloading class definitions of composed objects on unserialize() from session

    On May 14, 11:53 am, carrion <hofmann.johan. ..@gmail.comwro te:
    Hello everyone!
    I'm currently working on a MVC-framework and have run into an issue.
    >
    I'm using separate script files to render the active page's CSS and
    Javascript Code.
    I want to pull the data from the page controller object, which is
    initialized in another script.
    I thought this wouldn't be a problem if I simply stored the controller
    in the session-file, but the unserialization of the object simply
    won't work.
    The controller has properties that are of different class types than
    the controller itsself (page-secific Model and View objects).
    >
    My Questions:
    >
    1. Are there any problems using $myObj=$_SESSIO N['myObj']; ?
    I thought that as of PHP5 this should work?
    Not that I know of.
    >
    2. do class definitions of contained objects also have to be known
    when i unserialize?
    (like my Model/View objects)
    Yes.
    >
    3. can I get __autoload to help me out of this?
    Absolutely. I highly recommend the use of __autoload() for a lot of
    reasons, not the least of which is the problem you've described.

    function __autoload($cla ss_name) {
    $fn = $class_name . '.php';
    require_once $fn;
    }




    Comment

    • carrion

      #3
      Re: Autoloading class definitions of composed objects on unserialize() from session

      >...
      3. can I get __autoload to help me out of this?
      >
      Absolutely. I highly recommend the use of __autoload() for a lot of
      reasons, not the least of which is the problem you've described.
      >
      function __autoload($cla ss_name) {
      $fn = $class_name . '.php';
      require_once $fn;
      >
      }
      Thanks for the quick response.
      However, autoload() doesn't do what it should.

      The code below is about all that happens in the css skript.

      //START
      session_start() ;
      $objController= $_SESSION['controller'];
      var_dump($objCo ntroller);
      $objCssParser=n ew CssParser($objC ontroller);
      //END

      Now autoload() is triggered by the "new CSSParser" part and loads the
      whole hierarchy of CSSParser's parent classes and interfaces.
      But the unserializing doesn't bother it at all. The function is not
      called.

      So after that the type-hinting in CssParser's constructor causes an
      error to be thrown since it can't determine the class of
      $objController.

      Suggestions?

      Comment

      • ZeldorBlat

        #4
        Re: Autoloading class definitions of composed objects on unserialize() from session

        On May 14, 12:14 pm, carrion <hofmann.johan. ..@gmail.comwro te:
        ...
        3. can I get __autoload to help me out of this?
        >
        Absolutely. I highly recommend the use of __autoload() for a lot of
        reasons, not the least of which is the problem you've described.
        >
        function __autoload($cla ss_name) {
        $fn = $class_name . '.php';
        require_once $fn;
        >
        }
        >
        Thanks for the quick response.
        However, autoload() doesn't do what it should.
        >
        The code below is about all that happens in the css skript.
        >
        //START
        session_start() ;
        $objController= $_SESSION['controller'];
        var_dump($objCo ntroller);
        $objCssParser=n ew CssParser($objC ontroller);
        //END
        >
        Now autoload() is triggered by the "new CSSParser" part and loads the
        whole hierarchy of CSSParser's parent classes and interfaces.
        But the unserializing doesn't bother it at all. The function is not
        called.
        >
        So after that the type-hinting in CssParser's constructor causes an
        error to be thrown since it can't determine the class of
        $objController.
        >
        Suggestions?
        Try adding this immediately after you define __autoload():

        ini_set('unseri alize_callback_ func', '__autoload');

        Comment

        • carrion

          #5
          Re: Autoloading class definitions of composed objects on unserialize() from session

          On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gma il.comwrote:
          On May 14, 12:14 pm, carrion <hofmann.johan. ..@gmail.comwro te:
          >
          >
          >
          >...
          3. can I get __autoload to help me out of this?
          >
          Absolutely. I highly recommend the use of __autoload() for a lot of
          reasons, not the least of which is the problem you've described.
          >
          function __autoload($cla ss_name) {
          $fn = $class_name . '.php';
          require_once $fn;
          >
          }
          >
          Thanks for the quick response.
          However, autoload() doesn't do what it should.
          >
          The code below is about all that happens in the css skript.
          >
          //START
          session_start() ;
          $objController= $_SESSION['controller'];
          var_dump($objCo ntroller);
          $objCssParser=n ew CssParser($objC ontroller);
          //END
          >
          Now autoload() is triggered by the "new CSSParser" part and loads the
          whole hierarchy of CSSParser's parent classes and interfaces.
          But the unserializing doesn't bother it at all. The function is not
          called.
          >
          So after that the type-hinting in CssParser's constructor causes an
          error to be thrown since it can't determine the class of
          $objController.
          >
          Suggestions?
          >
          Try adding this immediately after you define __autoload():
          >
          ini_set('unseri alize_callback_ func', '__autoload');
          Good idea, that almost did the trick.
          I say almost since now i get one call to __autoload for the Controller
          class that is loaded.
          However that also doesn't take care of the inner objects.

          Yet... if i can load the containing class, maybe i can use it's
          __wakeup method to require the other files as needed...
          Anyone tried that? Or is there a chicken/egg problem?

          Greetings,

          Johannes

          Comment

          • carrion

            #6
            Re: Autoloading class definitions of composed objects on unserialize() from session

            On 14 Mai, 18:47, carrion <hofmann.johan. ..@gmail.comwro te:
            On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gma il.comwrote:
            >
            >
            >
            On May 14, 12:14 pm, carrion <hofmann.johan. ..@gmail.comwro te:
            >
            ...
            3. can I get __autoload to help me out of this?
            >
            Absolutely. I highly recommend the use of __autoload() for a lot of
            reasons, not the least of which is the problem you've described.
            >
            function __autoload($cla ss_name) {
            $fn = $class_name . '.php';
            require_once $fn;
            >
            }
            >
            Thanks for the quick response.
            However, autoload() doesn't do what it should.
            >
            The code below is about all that happens in the css skript.
            >
            //START
            session_start() ;
            $objController= $_SESSION['controller'];
            var_dump($objCo ntroller);
            $objCssParser=n ew CssParser($objC ontroller);
            //END
            >
            Now autoload() is triggered by the "new CSSParser" part and loads the
            whole hierarchy of CSSParser's parent classes and interfaces.
            But the unserializing doesn't bother it at all. The function is not
            called.
            >
            So after that the type-hinting in CssParser's constructor causes an
            error to be thrown since it can't determine the class of
            $objController.
            >
            Suggestions?
            >
            Try adding this immediately after you define __autoload():
            >
            ini_set('unseri alize_callback_ func', '__autoload');
            >
            Good idea, that almost did the trick.
            I say almost since now i get one call to __autoload for the Controller
            class that is loaded.
            However that also doesn't take care of the inner objects.
            >
            Yet... if i can load the containing class, maybe i can use it's
            __wakeup method to require the other files as needed...
            Anyone tried that? Or is there a chicken/egg problem?
            >
            Greetings,
            >
            Johannes
            by the way, i can't post for an hour or so.

            Comment

            • gosha bine

              #7
              Re: Autoloading class definitions of composed objects on unserialize()fr om session

              On 14.05.2007 17:53 carrion wrote:
              Hello everyone!
              I'm currently working on a MVC-framework and have run into an issue.
              >
              I'm using separate script files to render the active page's CSS and
              Javascript Code.
              I want to pull the data from the page controller object, which is
              initialized in another script.
              I thought this wouldn't be a problem if I simply stored the controller
              in the session-file, but the unserialization of the object simply
              won't work.
              This is actually far from "simple". PHP wasn't designed with object
              persistence in mind and your best bet is to follow its "share nothing"
              approach.
              The controller has properties that are of different class types than
              the controller itsself (page-secific Model and View objects).
              Just rebuild it from scratch using parameters passed in request or session.
              >
              My Questions:
              >
              1. Are there any problems using $myObj=$_SESSIO N['myObj']; ?
              I thought that as of PHP5 this should work?
              >
              2. do class definitions of contained objects also have to be known
              when i unserialize?
              (like my Model/View objects)
              Yes. See http://www.php.net/manual/en/languag...ialization.php
              >
              3. can I get __autoload to help me out of this?
              __autoload is a hack, you'd better stay away from it. ;)
              >
              4. if not, can i perform some sort of __sleep()/__wakeup() magic to
              achieve the same effect?
              IIRC, wakeup() is called when object is already constructed, not when
              the classes are loading.


              --
              gosha bine

              extended php parser ~ http://code.google.com/p/pihipi
              blok ~ http://www.tagarga.com/blok

              Comment

              • Schraalhans Keukenmeester

                #8
                Re: Autoloading class definitions of composed objects on unserialize() from session

                At Mon, 14 May 2007 09:47:15 -0700, carrion let his monkeys type:
                On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gma il.comwrote:
                >On May 14, 12:14 pm, carrion <hofmann.johan. ..@gmail.comwro te:
                >>
                >>
                >>
                >...
                3. can I get __autoload to help me out of this?
                >>
                Absolutely. I highly recommend the use of __autoload() for a lot
                of reasons, not the least of which is the problem you've described.
                >>
                function __autoload($cla ss_name) {
                $fn = $class_name . '.php';
                require_once $fn;
                >>
                }
                >>
                Thanks for the quick response.
                However, autoload() doesn't do what it should.
                >>
                The code below is about all that happens in the css skript.
                >>
                //START
                session_start() ;
                $objController= $_SESSION['controller']; var_dump($objCo ntroller);
                $objCssParser=n ew CssParser($objC ontroller); //END
                >>
                Now autoload() is triggered by the "new CSSParser" part and loads the
                whole hierarchy of CSSParser's parent classes and interfaces. But the
                unserializing doesn't bother it at all. The function is not called.
                >>
                So after that the type-hinting in CssParser's constructor causes an
                error to be thrown since it can't determine the class of
                $objController.
                >>
                Suggestions?
                >>
                >Try adding this immediately after you define __autoload():
                >>
                >ini_set('unser ialize_callback _func', '__autoload');
                >
                Good idea, that almost did the trick. I say almost since now i get one
                call to __autoload for the Controller class that is loaded.
                However that also doesn't take care of the inner objects.
                >
                >
                Yet... if i can load the containing class, maybe i can use it's __wakeup
                method to require the other files as needed... Anyone tried that? Or is
                there a chicken/egg problem?
                >
                Greetings,
                >
                Johannes
                Perhaps you can store class info (use get_class() on the parsed
                $objController inside the CssParser class definition) so you can
                reconstruct it. If not, the object becomes stdClass.

                Not my sharpest hour today, so I may be way off the mark here. Perhaps
                having another look at:
                http://www.php.net/manual/en/languag...ialization.php yields some
                useful insights.

                Sh.

                Comment

                • carrion

                  #9
                  Re: Autoloading class definitions of composed objects on unserialize() from session

                  On 14 Mai, 22:29, Schraalhans Keukenmeester <inva...@invali d.spam>
                  wrote:
                  At Mon, 14 May 2007 09:47:15 -0700, carrion let his monkeys type:
                  >
                  >
                  >
                  >
                  >
                  >
                  >
                  On 14 Mai, 18:36, ZeldorBlat <zeldorb...@gma il.comwrote:
                  On May 14, 12:14 pm, carrion <hofmann.johan. ..@gmail.comwro te:
                  >
                  >...
                  3. can I get __autoload to help me out of this?
                  >
                  Absolutely. I highly recommend the use of __autoload() for a lot
                  of reasons, not the least of which is the problem you've described.
                  >
                  function __autoload($cla ss_name) {
                  $fn = $class_name . '.php';
                  require_once $fn;
                  >
                  }
                  >
                  Thanks for the quick response.
                  However, autoload() doesn't do what it should.
                  >
                  The code below is about all that happens in the css skript.
                  >
                  //START
                  session_start() ;
                  $objController= $_SESSION['controller']; var_dump($objCo ntroller);
                  $objCssParser=n ew CssParser($objC ontroller); //END
                  >
                  Now autoload() is triggered by the "new CSSParser" part and loads the
                  whole hierarchy of CSSParser's parent classes and interfaces. But the
                  unserializing doesn't bother it at all. The function is not called.
                  >
                  So after that the type-hinting in CssParser's constructor causes an
                  error to be thrown since it can't determine the class of
                  $objController.
                  >
                  Suggestions?
                  >
                  Try adding this immediately after you define __autoload():
                  >
                  ini_set('unseri alize_callback_ func', '__autoload');
                  >
                  Good idea, that almost did the trick. I say almost since now i get one
                  call to __autoload for the Controller class that is loaded.
                  However that also doesn't take care of the inner objects.
                  >
                  Yet... if i can load the containing class, maybe i can use it's __wakeup
                  method to require the other files as needed... Anyone tried that? Or is
                  there a chicken/egg problem?
                  >
                  Greetings,
                  >
                  Johannes
                  >
                  Perhaps you can store class info (use get_class() on the parsed
                  $objController inside the CssParser class definition) so you can
                  reconstruct it. If not, the object becomes stdClass.
                  >
                  Not my sharpest hour today, so I may be way off the mark here. Perhaps
                  having another look at:http://www.php.net/manual/en/languag...tion.phpyields some
                  useful insights.
                  >
                  Sh.
                  Thanks for the input.
                  I got around the issue by rendering the css and js code in the main
                  view and storing them in the session as strings,
                  passing the session-id to the generator scripts via GET-Parameter.

                  Anyway I'm a bit dissapointet that there seems to be no reliable way
                  of serializing complex objects in PHP5...
                  Does anybody know if this will improve with PHP6 ?

                  Comment

                  Working...