title: Use of PHP Magic Methods __sleep and __wakeup
body: <p>What is the use of the <code>__sleep</code> and <code>__wakeup</code> magic methods in PHP? I read the PHP documentation but it's still not clear:</p>
  
  <pre><code>class sleepWakeup {
  
      public function __construct() {
          // constructor //
      }
  
      public function __sleep() {
          echo 'Time to sleep.';
      }
  
      public function __wakeup() {
          echo 'Time to wakeup.';
      }
  
  }
  
  $ob = new sleepWakeup();
  
  // call __sleep method
  echo $ob-&gt;__sleep();
  
  echo "\n";
  
  // call __wakeup method
  echo $ob-&gt;__wakeup();
  </code></pre>
  
  <p>This sample code prints:</p>
  
  <pre><code>Time to sleep.
  Time to wakeup.
  </code></pre>
  
  <p>If I were to rename <code>__sleep</code> and <code>__wakeup</code> to <code>foo</code> and <code>bar</code> then it does the same thing. What is the proper use of these two methods?</p>