Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:这是都是最基础的知识
<?php
class User
{
public $name;
public $money;
// 构造方法(实例化时自动调用)
public function __construct($name, $money)
{
$this->name = $name;
$this->money = $money;
// 调用get()方法
echo $this->get();
}
protected function get()
{
echo $this->name . ':' . $this->money . '元';
}
}
$user = new User('床垫', 6888);
<?php
class User
{
private $name;
private $money;
private $sale;
// 构造方法
public function __construct($name, $money, $sale)
{
$this->name = $name;
$this->money = $money;
$this->sale = $sale;
}
// __get()方法(当外部访问类中不存在或者无权限访问属性是会自动调用并处理)
public function __get($name)
{
// return $this->$name;
// 需要处理并调用相关方法,不然设置protected/private就无意义
$method = 'get' . ucfirst($name);
return method_exists($this, $method) ? $this->$method() : '无权限访问';
}
protected function getName()
{
// 可以过滤一些字符
echo mb_substr($this->name, 0, 11) . '...';
}
protected function getMoney()
{
// 直接输出折后价
echo '折后价' . $this->money * $this->sale;
}
}
$user = new User('手机,iphone11,256G', 8500, 0.8);
echo $user->name . '<br>', $user->money . '<br>', $user->sale . '<br>';
<?php
class User
{
private $name;
private $money;
private $sale;
// 构造方法
public function __construct($name, $money, $sale)
{
$this->name = $name;
$this->money = $money;
$this->sale = $sale;
}
// __get()方法
public function __set($name, $value)
{
// 调用统一方法
return $this->setName($name, $value);
}
// 修改名称时也可以调用统一方法 在输出时用__get()方法过滤或者其他操作
private function setName($name, $value)
{
return $this->$name = $value;
}
// __get()方法(当外部访问类中不存在或者无权限访问属性是会自动调用并处理)
public function __get($name)
{
$method = 'get' . ucfirst($name);
return method_exists($this, $method) ? $this->$method() : '无权限访问';
}
protected function getName()
{
// 可以过滤一些字符
echo mb_substr($this->name, 0, 11) . '...';
}
protected function getMoney()
{
// 直接输出折后价
echo '折后价' . $this->money * $this->sale;
}
}
$user = new User('手机,iphone11,256G', 8500, 0.9);
echo $user->name . ':' . $user->money . '元<hr>';
$user->name = '手机,iphone12,256G';
$user->money = 6000;
echo $user->name . ':' . $user->money . '元';
<?php
// __isset()方法检测私有成员
class User
{
public $name;
private $money;
private $sale;
public function __construct($name, $money)
{
$this->name = $name;
$this->money = $money;
}
public function __isset($name)
{
var_dump(isset($this->$name));
}
}
$user = new User('小龙虾', '38元/斤');
// isset检查变量是否初始化(对于不存在或无访问权限的的属性时时没有返回值的)
var_dump((isset($user->name)));
echo '<hr>';
var_dump((isset($user->sale)));
echo '<hr>';
echo (isset($user->money));
<?php
// __unset()方法删除私有成员
class User
{
public $name;
private $money;
private $sale;
public function __construct($name, $money, $sale)
{
$this->name = $name;
$this->money = $money;
$this->sale = $sale;
}
public function __get($name)
{
return $this->$name;
}
public function __unset($name)
{
unset($this->$name);
}
}
$user = new User('香烟', 100, '0.99');
echo $user->name . '<br>', $user->money . '<br>', $user->sale . '<br>';
// 删除公共成员
unset($user->name);
echo $user->name . '<br>', $user->money . '<br>', $user->sale . '<br>';
// 删除私有成员
unset($user->money);
echo $user->name . '<br>', $user->money . '<br>', $user->sale . '<br>';
<?php
class User
{
// (当外部调用类中不存在或者无权限访问方法时会自动调用并处理)
public function __call($name, $ary)
{
printf("方法名:%s ,参数为:[%s]", $name, implode(',', $ary));
}
// 当外部调用类中不存在或者无权限访问静态方法时会自动调用并处理)
public static function __callstatic($name, $ary)
{
printf("方法名:%s , 参数为:[%s]", $name, implode(',', $ary));
}
}
$user = new User;
$user->get(1, 2, 3);
echo '<hr>';
User::set(1, 2, 3);
<?php
// 事件委托(跨类)
class User
{
public function get($name, $ary)
{
printf("方法名:%s ,参数为:[%s]", $name, implode(',', $ary));
}
public static function set($name, $ary)
{
printf("方法名:%s , 参数为:[%s]", $name, implode(',', $ary));
}
}
class Work
{
private $user;
// 构造方法
public function __construct($name)
{
$this->user = $name;
}
// 访问未定义的普通方法时自动调用
public function __call($name, $ary)
{
if (method_exists($this->user, $name))
return call_user_func_array([$this->user, $name], [$name, $ary]);
}
// 访问未定义的静态方法时自动调用
public static function __callStatic($name, $ary)
{
if (method_exists('User', $name))
return call_user_func_array(['User', $name], [$name, $ary]);
}
}
$user = new User;
$work = new Work($user);
$work->get(1, 2, 3);
echo '<hr>';
Work::set(1, 2, 3, 4);
<?php
class Query
{
// 连接对象
protected $db;
// 数据表
protected $table;
// 字段列表
protected $field;
// 预处理字符
protected $ex;
// 构造方法创建数据库连接
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 ex($exe)
{
$this->ex = $exe;
return $this;
}
// 生成查询语句
protected function sesql()
{
return sprintf('SELECT %s FROM %s WHERE `id` >?', $this->field, $this->table,);
}
// 生成新增语句
protected function insql()
{
return sprintf('INSERT %s SET %s=? ,%s=?, %s=?', $this->table, ...$this->field);
}
// 新增更新语句
protected function upsql()
{
return sprintf("UPDATE %s SET %s=? ,%s=?, %s=? WHERE %s=?", $this->table, ...$this->field);
}
// 新增删除语句
protected function desql()
{
return sprintf("DELETE FROM %s WHERE %s=?", $this->table, $this->field, $this->ex);
}
// 执行sql语句
public function select($id)
{
switch ($id) {
// 执行查询操作
case 'se':
$stmt = $this->db->prepare($this->sesql());
// var_dump($stmt);
// die;
$stmt->execute([$this->ex]);
foreach ($stmt->fetchAll() as $a) {
vprintf('<li>序号:%d,姓名= %s 性别= %s</li><hr>', $a);
}
break;
// 执行新增操作
case 'in':
$stmt = $this->db->prepare($this->insql());
$stmt->execute(...[$this->ex]);
printf('新增成功 %s 条数据<hr>', $stmt->rowCount());
break;
// 执行更新操作
case 'up':
$stmt = $this->db->prepare($this->upsql());
$stmt->execute(...[$this->ex]);
printf('更新成功 %s 条数据<hr>', $stmt->rowCount());
break;
// 执行删除操作
case 'de':
$stmt = $this->db->prepare($this->desql());
$stmt->execute([$this->ex]);
printf('删除成功 %s 条数据<hr>', $stmt->rowCount());
break;
default:
echo '请求失败';
}
}
}
class DB
{
public static function __callStatic($name, $ary)
{
$dsn = 'mysql:host=localhost;dbname=user';
$username = 'root';
$password = 'root';
$query = new Query($dsn, $username, $password);
return call_user_func([$query, $name], ...$ary);
}
}
// 新增操作
DB::table('apple')->field(['username', 'password', 'sex'])->ex(['野猪套', sha1(333), '男'])->select('in');
// 更新操作
DB::table('apple')->field(['username', 'password', 'sex', 'id'])->ex(['黄金石像', sha1(444), '男', 45])->select('up');
// 删除操作
DB::table('apple')->field('id')->ex(2)->select('de');
// 查询操作
DB::table('apple')->field('id, username, sex')->ex(30)->select('se');
1.了解了拦截器的应用场景
2.对于事件委托有了一定的认识