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);

在“服务器”、“用户名”、“密码”、“数据库”属性“cnt”序列化后应该销毁。但是当我调用 $obj->connect(); 它给了我 $this->cnt 值....

请解释

4

1 回答 1

0

serialize() 处理所有类型,除了资源类型。因此从 mysql_connect 返回的资源不会被序列化。

于 2012-06-17T06:26:59.733 回答