0
請看下面給出的代碼。對象序列化 - 問題
class Connection
{
protected $link;
private $server, $username, $password, $db, $cnt;
public function __construct($server, $username, $password, $db)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->cnt = 1;
$this->connect();
}
public function connect()
{
echo '<br /> in connect '.($this->cnt++);
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
}
public function __sleep()
{
echo '<br />in sleep';
return array('server', 'username', 'password', 'db');
}
public function __wakeup()
{
echo '<br /> in wake up';
$this->connect();
}
}
$obj = new Connection('server', 'test', 'test', 'test');
$s = serialize($obj);
$obj->connect();
unserialize($s);
如果我沒有錯那麼,Serialize應該銷燬所有其他類的成員。
$s = serialize($obj);
$obj->connect();
unserialize($s);
'服務器','用戶名','密碼','db'屬性'cnt'的序列化應該被銷燬。但是當我調用$ obj-> connect();它給了我這個 - $> CNT值....
PLZ解釋
爲什麼序列化'服務器','用戶名','密碼','數據庫'屬性'cnt'應該銷燬?所有這些都會被摧毀? – Andreas
是的...但是當你在serialize之後調用connect時,它仍然連接到db和ptint cnt作爲2 ---這意味着變量不會被銷燬......它們是可用的 –