对象序列化 - 问题

时间:2012-06-17 06:22:03

标签: php php-5.3

请看下面给出的代码。

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-&gt; connect();它给了我$ this-&gt; cnt值......

Plz解释

1 个答案:

答案 0 :(得分:0)

serialize()处理除resource-type之外的所有类型。因此,mysql_connect返回的资源不会被序列化。