PHP Magic methods __wakeup() and __sleep() usage

The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which do not need to be saved completely. The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

<?php
class Sleep
{

  public $greeting = 'Hello, world!';

  public function __sleep()
  {
    echo 'Going to sleep!' . PHP_EOL;
    return array('greeting');
  }

  public function __wakeup()
  {
    echo $this->connect();
  }

  public function connect()
  {
    echo 'Wake up!' . PHP_EOL;
    echo $this->greeting . PHP_EOL;
  }

}

$obj = new Sleep();
$serialize = serialize($obj);
unserialize($serialize);
?>