1. __sleep()
和 __wakeup()
魔术方法
<?php
// __sleep()和__wakeup 和 序列化相关
// 1. 序列化可以序列化对象或数组
// 2. 反序列化:返回序列化之前的原始结构
// 3. 序列化主要用于数据的传输和保存
// $arr = [1,2,'lisa',false,'abc'];
// echo serialize($arr);
// 将数据保存到文件中
// file_put_contents('test.txt',serialize($arr));
// 拿到数据
// $str = file_get_contents('test.txt');
// echo $str;
// 还原数据
// print_r(unserialize($str));
// 序列化与魔术方法的应用
class Demo
{
public $username = '校长';
public $age = 60;
public $gender = 'male';
public $hobby = 'fish';
// 外部序列化类实例时自动调用,可以用来隐藏一些属性
public function __sleep()
{
// 只保存username和hobby,其他属性不会保存
return ['username','hobby'];
}
// 外部反序列化对象时,自动调用,也可以同步一些操作
public function __wakeup()
{
$this->hobby = 'Reading Book';
}
}
// 实例化Demo
$obj = new Demo();
// 序列化
// file_put_contents('demo.txt',serialize($obj));
$demo = file_get_contents('demo.txt');
echo $demo;
echo '<hr>';
// 反序列化
$obj2 = unserialize($demo);
echo $obj2->username.' 的爱好变为了 '.$obj2->hobby;
1.1 自定义小案例
<?php
// 案例:序列化时生成购物清单的总价格,反序列化时生成使用优惠券后的总价格
class Demo
{
// 商品种类
public $list1 = ['男士拖鞋','15'];
public $list2 = ['垃圾桶','10'];
public $list3 = ['台灯','80'];
// 每种商品购买的数量
public $num1;
public $num2;
public $num3;
// 优惠券
public $discount = 1;
// 传入的参数表示每种商品买的数量
public function __construct ($num1,$num2,$num3)
{
$this->num1 = $num1;
$this->num2 = $num2;
$this->num3 = $num3;
}
// 序列化类实例时
public function __sleep()
{
echo '购物总金额是: '. ($this->list1[1]*$this->num1 + $this->list2[1]*$this->num2 + $this->list3[1]*$this->num3)*$this->discount.'<hr>';
return ['list1','list2','list3','list4','discount','num1','num2','num3'];
}
// 反序列化时将优惠调至八折
public function __wakeup()
{
$this->discount = 0.8;
}
}
// 类实例
$obj = new Demo(0,2,1);
// 序列化
$str = serialize($obj);
// 反序列化
$obj2 = unserialize($str);
echo '优惠后 购物总金额是: '. ($obj2->list1[1]*$obj2->num1 + $obj2->list2[1]*$obj2->num2 + $obj2->list3[1]*$obj2->num3)*$obj2->discount;
2. 匿名类操作数据库
操作的数据库如下:
2.1 匿名类查询操作
<?php
// 匿名类查询操作
interface iDB
{
public function __construct(...$params);
}
// 1. pdo方式
$select = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB {
private $pdo;
public function __construct (...$params)
{
$this->pdo = new PDO($params[0],$params[1],$params[2]);
}
public function select ()
{
$r = $this->pdo->query("SELECT `username`,`password`,`email` FROM `users`")->fetchAll(PDO::FETCH_ASSOC);
$this->pdo = null;
return $r;
}
}) -> select();
foreach ($select as $v) {
vprintf('| 用户名: %s | 密码: %s | 邮箱: %s <br>',$v);
}
echo '<hr>';
// 2. mysqli方式
$res = (new class ('php.edu','root','root','test') implements iDB {
private $mysqli;
public function __construct (...$params)
{
$this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
}
public function select ()
{
$r = $this->mysqli->query("SELECT `username`,`password`,`email` FROM `users`")->fetch_all(MYSQLI_ASSOC);
$this->mysqli->close();
return $r;
}
}) -> select();
foreach ($res as $v) {
vprintf('| 用户名: %s | 密码: %s | 邮箱: %s <br>',$v);
}
2.2 匿名类新增操作
<?php
// 匿名类新增数据到数据表
interface iDB
{
public function __construct (...$params);
}
// 1. pdo方式
$insert1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
{
private $pdo;
public function __construct (...$params)
{
$this->pdo = new PDO($params[0],$params[1],$params[2]);
}
public function insert1 ()
{
$this->pdo->query("INSERT INTO `users` (`username`,`password`,`email`) VALUES ('哈哈',sha1('123456'),'haha@qq.com')");
$this->pdo = null;
}
})->insert1();
// 2. mysqli方式
$insert2 = (new class ('php.edu','root','root','test') implements iDB
{
private $mysqli;
public function __construct (...$params)
{
$this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
}
public function insert2 ()
{
$this->mysqli->query("INSERT INTO `users` (`username`,`password`,`email`) VALUES ('小花',sha1('aaaaaa'),'xiaohua@qq.com')" );
$this->mysqli->close();
}
})->insert2();
2.3 匿名类更新操作
<?php
// 匿名类更新操作
interface iDB
{
public function __construct (...$params);
}
// 1. pdo方式
$update1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
{
private $pdo;
public function __construct (...$params)
{
$this->pdo = new PDO($params[0],$params[1],$params[2]);
}
public function update1 ()
{
$this->pdo->query("UPDATE `users` SET `username`='旺财',`email`='wangcai@qq.com' WHERE `id`=4");
$this->pdo = null;
}
})->update1();
// 2. mysqli方式
$update2 = (new class ('php.edu','root','root','test') implements iDB
{
private $mysqli;
public function __construct (...$params)
{
$this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
}
public function update2 ()
{
$this->mysqli->query("UPDATE `users` SET `username`='牛郎',`email`='niulang@qq.com' WHERE `id`=3");
$this->mysqli->close();
}
})->update2();
2.4 匿名类删除操作
<?php
// 匿名类删除操作
interface iDB
{
public function __construct (...$params);
}
// 1. pdo方式
$delete1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
{
private $pdo;
public function __construct (...$params)
{
$this->pdo = new PDO($params[0],$params[1],$params[2]);
}
public function delete1 ()
{
$this->pdo->query("DELETE FROM `users` WHERE `id`=1");
$this->pdo = null;
}
})->delete1();
// 2. mysqli方式
$delete2 = (new class ('php.edu','root','root','test') implements iDB
{
private $mysqli;
public function __construct (...$params)
{
$this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
}
public function delete2 ()
{
$this->mysqli->query("DELETE FROM `users` WHERE `username`='def'");
$this->mysqli->close();
}
})->delete2();