_sleep and _wakeup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Water Cooler v2

    #1

    _sleep and _wakeup

    I didn't understand from this

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    whether we, the programmer, have to implement _sleep and _wakeup (like
    ..NET's IDisposable.Dis pose()) or it is done by PHP internally on a
    class that uses resources?

  • AlexVN

    #2
    Re: _sleep and _wakeup

    Water Cooler v2 wrote:[color=blue]
    > I didn't understand from this
    >
    > http://in2.php.net/manual/en/languag...-functions.php
    >
    > whether we, the programmer, have to implement _sleep and _wakeup (like
    > .NET's IDisposable.Dis pose()) or it is done by PHP internally on a
    > class that uses resources?[/color]

    Hi,

    __sleep and __wakeup are the methods, that you can implement in your
    class. They are just like events that occur before serialization and
    after unserialization . Consider this:

    class MyClass {
    function __sleep() {
    echo "__sleep\n" ;
    return array();
    }
    function __wakeup() {
    echo "__wakeup\n ";
    }
    }

    $c = new MyClass();
    echo "Sleep: ";
    $str = serialize($c);
    echo "Wakeup: ";
    $c1 = unserialize($st r);

    The output will be:
    Sleep: __sleep
    Wakeup: __wakeup

    Sincerely,
    Alexander
    http://www.alexatnet.com/

    Comment

    • Water Cooler v2

      #3
      Re: _sleep and _wakeup

      Thanks. That answers it. The onus of cleaning up and restoring
      "resources" (if the class uses any) is with us, and hence the
      implementation of these methods is a way given to us to do that before
      serialization and after deserialization .

      Thanks, again. :-)

      Comment

      • Chung Leong

        #4
        Re: _sleep and _wakeup


        Water Cooler v2 wrote:[color=blue]
        > Thanks. That answers it. The onus of cleaning up and restoring
        > "resources" (if the class uses any) is with us, and hence the
        > implementation of these methods is a way given to us to do that before
        > serialization and after deserialization .
        >
        > Thanks, again. :-)[/color]

        You misunderstood I'm afraid. IDisposable.Dis pose called, if I remember
        my .NET correctly, when an object is garbage collected. __sleep() is
        called when an object is serialized--usually at the end of the request
        when PHP saves session data to a file. Its main purpose is to let PHP
        know what properties within the object should be saved and what
        shouldn't be (cached records for instance). The __wakeup() method is
        the inverse of that--called when PHP restore data previously stored in
        a file into an object.

        __destruct() is the closest counter part to IDisposable.Dis pose. You
        typically don't need to manually free resources, since PHP will do it
        for you at the end of a request.

        Comment

        • Water Cooler v2

          #5
          Re: _sleep and _wakeup


          Chung Leong wrote:
          [color=blue]
          > You misunderstood I'm afraid. IDisposable.Dis pose called, if I remember
          > my .NET correctly, when an object is garbage collected. __sleep() is
          > called when an object is serialized--usually at the end of the request
          > when PHP saves session data to a file. Its main purpose is to let PHP
          > know what properties within the object should be saved and what
          > shouldn't be (cached records for instance). The __wakeup() method is
          > the inverse of that--called when PHP restore data previously stored in
          > a file into an object.
          >
          > __destruct() is the closest counter part to IDisposable.Dis pose. You
          > typically don't need to manually free resources, since PHP will do it
          > for you at the end of a request.[/color]


          Thanks, Chung. I'd understood correctly, albeit my anology was rather
          amiss.

          Comment

          Working...