Difference between __sleep and __wakeup in php


__sleep returns the array of all the variables than need to be saved.
__wakeup retrieves them.
__sleep() and __wakeup() run prior to serialize() and unserialize() respectively.

Example:

<?php
class DBConn
{
    protected $conn;
    private $host, $username, $password, $db;
    
    public function __construct($host, $username, $password, $db)
    {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->connect();
    }
    
    private function connect()
    {
        $this->conn = mysql_connect($this->host, $this->username, $this->password);
        mysql_select_db($this->db, $this->conn);
    }
    
    public function __sleep()
    {
        return array('host', 'username', 'password', 'db');
    }
    
    public function __wakeup()
    {
        $this->connect();
    }
   
    $c=new DBConn("localhost","root","","testdb");
    $c->__wakeup();


}
?>