1. 属性拦截器
<?php
// 属性重载,又叫属性拦截器
// 示例一:__get():读取不可访问属性的值时自动调用
class Demo1
{
public function __get($name)
{
return $name.'<br>';
}
}
$obj1 = new Demo1();
echo $obj1->email;
echo $obj1->age;
echo '<hr>';
// 示例二:__set():在给不可访问属性赋值时自动调用
class Demo2
{
public function __set($name,$value)
{
echo $name.' : '.$value;
}
}
$obj2 = new Demo2();
$obj2->username = '小明';
echo '<hr>';
// 示例三:__isset():当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用
class Demo3
{
public $username = '孙悟空';
public function __isset($name)
{
echo $name.' 存在吗?'.'<br>';
return isset($this->$name);
}
}
$obj3 = new Demo3();
var_dump(isset($obj3->username));
echo '<hr>';
var_dump(isset($obj3->email));
echo '<hr>';
// 示例四:__unset():当对不可访问属性调用 unset() 时,__unset() 会被调用
class Demo4
{
public $num1 = 'aaa';
private $num2 = 'bbb';
public function __unset($name)
{
// 通过外部传参,可以输出私有成员$num2
echo $this->$name,'<hr>';
// 删除$num2变量
unset($this->$name);
// 现在就不能输出了,此时就能达到即使在外部也能访问私有成员,并操作它
echo $this->$name,'<hr>';
}
}
$obj4 = new Demo4();
// 实例化类得到对象,访问类中公共成员,然后删除$num1变量,没有问题
echo $obj4->num1,'<hr>';
unset($obj4->num1);
echo $obj4->num1,'<hr>';
// 但是不能访问私有成员,更别说在外部删除私有变量了,但是可以通过unset()去自动调用类中的__unset()魔术方法
// echo $obj4->num2;
unset($obj4->num2);
2. 查询构造器
操作的数据表如下:
2.1 select查询
<?php
// SELECT `字段` FROM `数据表` LIMIT `几条记录`
// 查询类
class Query
{
// 1. 连接对象
protected $pdo;
// 2. 字段
protected $field;
// 3. 数据表名
protected $table;
// 4. 查询几条
protected $limit;
// 5. 连接数据库:使用构造方法
public function __construct($dsn,$username,$password)
{
$this->connect($dsn,$username,$password);
}
private function connect($dsn,$username,$password)
{
$this->pdo = new PDO($dsn,$username,$password);
}
// 6. 设置默认查询字段。6,7,8步骤都设置return $this;是便于链式调用
public function field ($field)
{
$this->field = $field;
return $this;
}
// 7. 设置默认数据表名
public function table ($table)
{
$this->table = $table;
return $this;
}
// 8. 设置默认查询记录数量
public function limit ($limit)
{
$this->limit = $limit;
return $this;
}
// 9. 设置sql查询语句
public function sql ()
{
return sprintf(' SELECT %s FROM %s LIMIT %s ', $this->field , $this->table , $this->limit);
}
// 10. 执行查询语句
public function select ()
{
return $this->pdo->query($this->sql())->fetchAll(PDO::FETCH_ASSOC);
}
}
// 数据库操作类
class DB
{
public static function __callStatic ($name,$args)
{
$dsn = 'mysql:host=php.edu;dbname=first';
$username = 'root';
$password = 'root';
$query = new Query($dsn,$username,$password);
return call_user_func([$query,$name],...$args);
}
}
$res = DB::table('student') -> field('username,sex,tel') -> limit(3) -> select();
if (!empty($res)) {
$table = <<<EOF
<table border=1>
<tr>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
</tr>
EOF;
foreach ( $res as $key=>$value ) {
$table.="<tr>";
$table.="<td>{$value['username']}</td>";
$table.="<td>{$value['sex']}</td>";
$table.="<td>{$value['tel']}</td>";
$table.="</tr>";
}
$table.="</table>";
echo $table;
}
2.2 insert插入
<?php
// INSERT INTO 数据表 (字段) VALUES (插入值)
class Query
{
protected $pdo;
protected $field;
protected $table;
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 field ($field)
{
$this->field = $field;
return $this;
}
public function table ($table)
{
$this->table = $table;
return $this;
}
public function values ($values)
{
$this->values = $values;
return $this;
}
public 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=php.edu;dbname=first';
$username = 'root';
$password = 'root';
$query = new Query($dsn,$username,$password);
return call_user_func([$query,$name],...$args);
}
}
DB::table('student') -> field('username,password,sex,age,tel') -> values("'哇哇','123456','男','50','15050355655'") -> insert();
2.3 update更新
<?php
// UPDATE 数据表 SET 字段1=value WHERE 字段2=where
class Query
{
protected $pdo;
protected $field;
protected $set;
protected $table;
protected $value;
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 set ($set)
{
$this->set = $set;
return $this;
}
public function table ($table)
{
$this->table = $table;
return $this;
}
public function value ($value)
{
$this->value = $value;
return $this;
}
public function field ($field)
{
$this->field = $field;
return $this;
}
public function where ($where)
{
$this->where = $where;
return $this;
}
public function sql ()
{
return sprintf( " UPDATE %s SET %s = %s WHERE %s = %s", $this->table , $this->set , $this->value , $this->field , $this->where ) ;
}
public function update ()
{
// var_dump($this->pdo->query($this->sql()));
$this->pdo->query($this->sql());
}
}
class DB
{
public static function __callStatic ($name,$args)
{
$dsn = 'mysql:host=php.edu;dbname=first';
$username = 'root';
$password = 'root';
$query = new Query($dsn,$username,$password);
return call_user_func([$query,$name],...$args);
}
}
DB::table('student') -> set('username') -> value("'老五'") -> field('id') -> where("'4'") -> update();
2.4 delete删除
<?php
// DELETE FROM 数据表 WHERE 字段=where
class Query
{
protected $pdo;
protected $field;
protected $table;
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 field ($field)
{
$this->field = $field;
return $this;
}
public function table ($table)
{
$this->table = $table;
return $this;
}
public function where ($where)
{
$this->where = $where;
return $this;
}
public function sql ()
{
return sprintf( " DELETE FROM %s WHERE %s=%s ", $this->table , $this->field ,$this->where);
}
public function delete ()
{
$this->pdo->query($this->sql());
}
}
class DB
{
public static function __callStatic ($name,$args)
{
$dsn = 'mysql:host=php.edu;dbname=first';
$username = 'root';
$password = 'root';
$query = new Query($dsn,$username,$password);
return call_user_func([$query,$name],...$args);
}
}
DB::table('student') -> field('id') -> where("'1015'") -> delete();