Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:我们学得比较多, 用得时候根据场景再定
__sleep()
,wakeup()
// 序列化
$str1 = 'php.cn';
$str2 = serialize($str1);
$str3 = unserialize($str2);
echo '序列化前:' . $str1 . '<br>';
echo '序列化后:' . $str2 . '<br>';
echo '反序列化:' . $str3;
echo '<hr>';
$arr1 = ['php.cn', 'PHP中文网', true, false, 1, 2, 3];
$arr2 = serialize($arr1);
$arr3 = unserialize($arr2);
echo '序列化前:' . $arr1 . '<br>';
echo '序列化后:' . $arr2 . '<br>';
echo '反序列化:<br>';
print_r($arr3);
echo '<hr>';
class Demo
{
public $name = 'Peter';
public $age = 38;
public $email = 'peter@php.cn';
// 当外部序列化类实例的时候,自动调用它,用来隐藏一些属性
// serialize($this)被调用的时候自动执行它
public function __sleep(): array
{
return ['name', 'email'];
}
// 外部反序列化这个对象的时候,会被自动调用
public function __wakeup()
{
// 在唤醒一个对象的时候,可以同步执行一些操作
$this->email = 'peter@java.com';
}
}
$demo = new Demo();
// 序列化数据的目的是将数据进行传输或保存
file_put_contents('obj.txt', serialize($demo));
$str = file_get_contents('obj.txt');
echo $str . '<hr>';
// 反序列化
// string ===> object
$obj = unserialize($str);
// echo gettype($obj);
echo '姓名:' . $obj->name . ': 邮箱: ' . $obj->email . ', 年龄 : ', $obj->age;
// 数据库接口,构造方法自动实例化数据库
// $type为数据库类型,同时支持PDO与mysqli
//...$params为连接参数
interface iDb
{
public function __construct($type, ...$params);
}
class Db implements iDb
{
private $type;
private $db;
public function __construct($type, ...$params)
{
$this->type = $type;
if ($type === 'PDO') {
$this->db = new PDO($params[0], $params[1], $params[2]);
} elseif ($type === 'MYSQLI') {
$this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
} else {
return '未知数据库类型!';
}
}
public function select()
{
if($this->type==='PDO'){
return $this->db->query('select * from users limit 3')->fetchAll(PDO::FETCH_ASSOC);
}elseif($this->type==='MYSQLI'){
return $this->db->query('select * from users limit 2')->fetch_all(MYSQLI_ASSOC);
}else{
return '未知数据库类型!';
}
}
public function insert()
{
return $this->db->query("INSERT `users` SET `username`='arwang666', `password`='arwang666';");
}
public function update()
{
return $this->db->query("UPDATE `users` SET `username`='PETER', `password`='PETER' WHERE `id`=6;");
}
public function delete()
{
return $this->db->query("DELETE FROM `users` WHERE `id`=6;");
}
}
// 客户端代码:
// 使用PDO:
// $demo = new Db('PDO', 'mysql:host=localhost;dbname=php12', 'root', 'root');
// 使用mysqli
$demo = new Db('MYSQLI','localhost', 'root', 'root', 'php12');
var_dump($demo->select());
var_dump($demo->insert());
var_dump($demo->update());
var_dump($demo->delete());
// 数据库接口,构造方法自动实例化数据库
// $type为数据库类型,同时支持PDO与mysqli
//...$params为连接参数
interface iDb
{
public function __construct($type, ...$params);
}
// 注意:实例化类的时候第一个为数据库类型:pdo或mysqli
// PDO
// $user = (new class ('PDO','mysql:host=localhost;dbname=php12', 'root', 'root') implements iDb{
// MYSQLI
$user = (new class ('MYSQLI','localhost', 'root', 'root', 'php12') implements iDb{
private $type;
private $db;
public function __construct($type, ...$params)
{
$this->type = $type;
if ($type === 'PDO') {
$this->db = new PDO($params[0], $params[1], $params[2]);
} elseif ($type === 'MYSQLI') {
$this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
} else {
return '未知数据库类型!';
}
}
public function select()
{
if($this->type==='PDO'){
return $this->db->query('select * from users limit 3')->fetchAll(PDO::FETCH_ASSOC);
}elseif($this->type==='MYSQLI'){
return $this->db->query('select * from users limit 2')->fetch_all(MYSQLI_ASSOC);
}else{
return '未知数据库类型!';
}
}
public function insert()
{
return $this->db->query("INSERT `users` SET `username`='arwang666', `password`='arwang666';");
}
public function update()
{
return $this->db->query("UPDATE `users` SET `username`='PETER', `password`='PETER' WHERE `id`=6;");
}
public function delete()
{
return $this->db->query("DELETE FROM `users` WHERE `id`=6;");
}
});
// 增删改查只需要修改$user的对应的方法
var_dump($user->select());
数据库仅为3个字段,输出结果请需要的同学复制代码自行测试。