Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:理解了, 很好
<?php
// __get(), __set(), __isset(), __unset()用法
// 1.__get($name):当外部访问一个不存在或无权限访问的属性时,自动调用
class Product1
{
public function __get($name)
{
return $name;
}
}
$obj1 = new Product1();
echo $obj1->name;
echo $obj1->price;
echo '<hr>';
// 2.__set()属性更新操作重定向
class Product2
{
public function __set($name, $value)
{
echo $name. ':' .$value;
}
}
$obj2 = new Product2();
$obj2->username = '小芳';
echo '<hr>';
//3. __isset()检查属性
class Product3
{
public $username = '小刘';
public function __isget($name)
{
echo $name. '小李';
return isset($this->$name);
}
}
$obj3 = new Product3();
var_dump(isset($obj3->username));
echo '<hr>';
var_dump(isset($obj3->price));
// 4.__unset()删除属性
class Product4
{
public $name1 = '小明';
private $name2 = '小六';
public function __unset($name)
{
echo $this->$name ,'<hr>';
// 访问内部私有属性
unset($this->$name);
echo $this->$name ,'<hr>';
}
}
$obj4 = new Product4();
// 访问公共成员
echo '<hr>';
echo $obj4->name1;
echo '<hr>';
// 删除公共成员
unset($obj4->name1);
echo '<hr>';
// 外部访问内部私有属性
echo $obj4->name1;
unset($obj4->name2);
select()
:方法
<?php
// 方法委托/方法拦截器 实战: 数据库查询构造器(链式查询)
// 查询类
class Query
{
// 连接对象
protected $db;
// 数据表
protected $table;
// 字段列表
protected $field;
// 记录数量
protected $limit;
// 构造方法: 连接数据库
public function __construct($dsn, $username, $password)
{
$this->connect($dsn, $username, $password);
}
// 连接数据库
private function connect($dsn, $username, $password)
{
$this->db = new PDO($dsn, $username, $password);
}
// 设置默认的数据表名称
public function table($table)
{
$this->table = $table;
return $this;
}
// 设置默认的字段名称
public function field($field)
{
$this->field = $field;
return $this;
}
// 链式方法: 设置查询数量
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
// 生成查询语句
protected function getSql()
{
return sprintf('SELECT %s FROM %s LIMIT %s', $this->field, $this->table, $this->limit);
}
// 执行查询
public function select()
{
return $this->db->query($this->getSql())->fetchAll(PDO::FETCH_ASSOC);
}
}
// 数据据操作类
class DB
{
// 静态方法委托
public static function __callStatic($name, $args)
{
// 获取到查询类的对象: new Query()
$dsn = 'mysql:host=localhost;dbname=phpedu';
$username = 'root';
$password = 'melinda123';
$query = new Query($dsn, $username, $password);
// 直接跳到Query类中的具体方法来调用
return call_user_func([$query, $name], ...$args);
}
}
$result = DB::table('users')
// 字段列表
->field('id,name,email')
->limit(2)
// 开始查询
->select();
print_r($result);
inset()
:方法
<?php
//数据库插入构造器
class Query
{
protected $pdo;
protected $table;
protected $field;
protected $values;
// 构造方法: 连接数据库
public function __construct($dsn, $username, $password)
{
$this->connect($dsn, $username, $password);
}
private function connect($dsn, $username, $password)
{
$this->pdo = new PDO($dsn, $username, $password);
}
public function table ($table)
{
$this->table = $table;
return $this;
}
public function field ($field)
{
$this->field = $field;
return $this;
}
public function values ($values)
{
$this->values = $values;
return $this;
}
// 生成插入语句
protected function sql()
{
return sprintf(' INSERT INTO %s (%s) VALUES (%s) ', $this->table, $this->field, $this->values);
}
// 执行
public function insert()
{
$this->pdo->query($this->sql());
}
}
class DB
{
public static function __callStatic($name, $args)
{
$dsn = 'mysql:host=localhost;dbname=phpedu';
$username = 'root';
$password = 'melinda123';
$query = new Query($dsn, $username, $password);
return call_user_func([$query, $name], ...$args);
}
}
$result = DB::table('users')
-> field(`id,username,email,password`)
-> values("'飞猪', '123@qq.com', 'fz123'")
-> insert();
echo $result ? '添加成功' :"添加失败";
update()
:方法
<?php
//数据库更新构造器
class Query
{
protected $pdo;
protected $table;
protected $set;
protected $field;
protected $values;
protected $where;
// 构造方法: 连接数据库
public function __construct($dsn, $username, $password)
{
$this->connect($dsn, $username, $password);
}
// 连接数据库
private function connect($dsn, $username, $password)
{
$this->pdo = new PDO($dsn, $username, $password);
}
// 设置默认的数据表名称
public function table ($table)
{
$this->table = $table;
return $this;
}
public function set ($set)
{
$this->set = $set;
return $this;
}
// 设置默认的字段名称
public function field ($field)
{
$this->field = $field;
return $this;
}
public function values ($values)
{
$this->values = $values;
return $this;
}
public function where ($where)
{
$this->where = $where;
return $this;
}
// 生成插入语句
protected function sql ()
{
return sprintf("UPDATE %s SET %s =%s WHERE %s = %s", $this->field, $this->table, $this->values, $this->set, $this->where);
}
// 执行
public function update ()
{
$this->pdo->query($this->sql());
}
}
// 数据据操作类
class DB
{
// 静态方法委托
public static function __callStatic($name, $args)
{
$dsn = 'mysql:host=localhost;dbname=phpedu';
$username = 'root';
$password = 'melinda123';
$query = new Query($dsn, $username, $password);
return call_user_func([$query, $name], ...$args);
}
}
$result = DB::table('users')
->set('username')
->values("'天猫'")
->field('id')
->where("'7'")
->update();
echo $result ? '更新成功' :"更新失败";
delete()
:方法
<?php
//数据库删除构造器
class Query
{
protected $pdo;
protected $table;
protected $field;
protected $where;
protected $values;
// 构造方法: 连接数据库
public function __construct($dsn, $username, $password)
{
$this->connect($dsn, $username, $password);
}
// 连接数据库
private function connect($dsn, $username, $password)
{
$this->pdo = new PDO($dsn, $username, $password);
}
// 设置默认的数据表名称
public function table ($table)
{
$this->table = $table;
return $this;
}
// 设置默认的字段名称
public function field ($field)
{
$this->field = $field;
return $this;
}
public function where ($where)
{
$this->where = $where;
return $this;
}
public function values ($values)
{
$this->values = $values;
return $this;
}
// 生成插入语句
protected function sql ()
{
return sprintf(' DELETE FROM %s WHERE %s = %s', $this->table, $this->field, $this->values);
}
// 执行
public function delete()
{
return $this->pdo->query($this->sql());
}
}
// 数据据操作类
class DB
{
// 静态方法委托
public static function __callStatic($name, $args)
{
$dsn = 'mysql:host=localhost;dbname=phpedu';
$username = 'root';
$password = 'melinda123';
$query = new Query($dsn, $username, $password);
return call_user_func([$query, $name], ...$args);
}
}
$result = DB::table('users')
->field('id')
->values(32)
->delete();
echo $result ? '删除成功' :'删除失败';