1.sleep(), wakeup()实战案例
- 序列化时,属性里有空格也当作一个字符串,多个数组也能序列化
- 当使用__wakeup(),修改了里面的价格,外部反序列化unserialize的时候,就会得到新价格,理解为唤醒
<?php
// 序列化
echo serialize('me linda'), '<br>';
echo serialize([1,2,false,null]);
echo '<hr>';
class User
{
public $name = '手机';
public $price = '5000';
public $num = 2;
// 把一些属性隐藏,当serialize($this)被调用时自动执行它
public function __sleep(): array
{
return ['name', 'price'];
}
// 外部反序列化这个对象时,会被自动调用
public function __wakeup()
{
$this->price = 6000;
}
}
$user = new User();
// echo $str;
// 序列化数据为了使当前数据进行传输或保存
file_put_contents('obj.txt', serialize($user));
$str = file_get_contents('obj.txt');
// echo $str;
// gettype后得到的是string,那么如何把它反序列化操作?
echo gettype($str);
echo '<hr>';
// 反序列化,把string转为object
$obj = unserialize($str);
echo gettype($obj);
echo $obj->name . ':价格:' .$obj->price . ', 数量:' , $obj->num;
2.PDO和匿名类查询、新增、更新、 删除方法
1.pdo连接后,select查询2条数据
2.匿名类改造pdo后,查询2条数据
3.匿名类改造pdo后,update更新1条数据
4.匿名类改造pdo后,insert插入1条id为50的数据
5.匿名类改造pdo后,delete删除1条id为29的数据
<?php
// 匿名类应用场景
// 实现接口的部分功能
interface iDb
{
public function __construct(...$params);
}
// 2.pdo实现
class My_pdo implements iDb
{
private $db = null;
public function __construct(...$params)
{
$this->db = new PDO($params[0], $params[1],$params[2]);
}
public function select()
{
return $this->db->query('select * from users limit 2')->fetchAll(PDO::FETCH_ASSOC);
}
}
$mypdo = new My_pdo('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123');
print_r($mypdo->select());
echo '<hr>';
// 匿名类改造pdo实现的接口
$users = (new class ('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123') implements iDb {
private $db = null;
public function __construct(...$params)
{
$this->db = new PDO($params[0], $params[1],$params[2]);
}
public function select()
{
return $this->db->query('select * from users limit 2')->fetchAll(PDO::FETCH_ASSOC);
}
})->select();
print_r($users);
echo '<hr>';
$us = (new class ('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123') implements iDb {
private $db = null;
public function __construct(...$params)
{
$this->db = new PDO($params[0], $params[1],$params[2]);
}
public function update()
{
return $this->db->query('UPDATE users SET ID = 3 WHERE ID = 40')->rowCount();
}
})->update();
print_r($us);
echo '<hr>';
$us = (new class ('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123') implements iDb {
private $db = null;
public function __construct(...$params)
{
$this->db = new PDO($params[0], $params[1],$params[2]);
}
public function insert()
{
return $this->db->query("INSERT INTO users (id, name, email, password) VALUES ('50','北京','bj@php.cn','bj123')")->rowCount();
}
})->insert();
print_r($us);
echo '<hr>';
$us = (new class ('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123') implements iDb {
private $db = null;
public function __construct(...$params)
{
$this->db = new PDO($params[0], $params[1],$params[2]);
}
public function delete()
{
return $this->db->query('DELETE FROM users WHERE ID = 29')->rowCount();
}
})->delete();
print_r($us);
- 总结:
- __sleep(): array把一些属性隐藏,当serialize($this)被调用时自动执行它
- __wakeup():显示一些属性,外部反序列化unserialize($this)这个对象时,会被自动调用
- 匿名类应用可以实现数据库接口的部分功能,比如数据库增删改查