What’s the special meaning of __sleep and __wakeup?

Posted on

__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

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();
}
}
?>

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s