class db {
private $host;
private $user;
private $pwd;
private $dbname;
private $Mysqli;
function __construct($host, $user, $pwd, $dbname) {
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
$this->dbname = $dbname;
$this->db();
}
function db() {
$this->mysqli = new mysqli ( $this->host, $this->user, $this->pwd, $this->dbname );
}
function select() {
$this->mysqli->query("SET CHARSET GBK");
$sql = "SELECT id,cname FROM hdw_channel";
$result = $this->mysqli
->query ( $sql );
$rows = array ();
while ( $row = $result->fetch_assoc () ) {
$rows [] = $row;
}
ECHO "";
print_r ( $rows );
}
function __wakeup(){ //反序列化,
$this->db();
}
}
$chanel = new db("localhost",'root','','hdcms');
//$chanel->select();
session_start();
$_SESSION['channel_obj'] = serialize($chanel); //将对象序列化,保存的是对象的属性,没有方法,所以要用__wakeup()
class ren{
private $name;
private $age;
function __construct($name,$age){
$this->name =$name;
$this->age = $age;
}
function show(){
echo "姓名是:{$this->name} 年龄是:{$this->age}";
}
function __sleep(){
return array_keys(get_object_vars($this)); //或得数组里边的键名,序列化某些变量
}
}
$zao = new ren("赵六",44);
echo serialize($zao); //序列化(指定哪个变量序列化)
====================================
session_start();
include '59.php';
$channel_obj=unserialize($_SESSION['channel_obj']); //反序列化类对象
$channel_obj->select(); //有了__wakeup方法才可以起作用