批改状态:合格
老师批语:随着课程深入, 内容会越来越抽象,不要缺课
1.代码
<?php//查询类class Query{//数据库连接protected $db;//表名protected $table;//字段protected $filed;//条件protected $where;//数据条数protected $limit;//修改字段protected $set;//构造方法连接数据库public function __construct($dsn,$username,$password){$this ->connert($dsn,$username,$password);}//数据库连接protected function connert($dsn,$username,$password){try{//连接数据款$this->db = new PDO($dsn,$username,$password);} catch(PDOException $e){//捕捉特定于数据库信息的PDOEXCEPTION 异常echo $e->getMessage();} catch(Throwable $e){//捕捉拥有Throwable接口的错误或者其他异常echo $e->getMessage();}}// 设置默认的数据表名称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;}// 设置查询条件public function where($where){$this->where = $where;return $this;}// 设置编辑字段public function set($set){$this->set = $set;return $this;}// 执行查询public function select(){$sql = sprintf('SELECT %s FROM %s WHERE %s LIMIT %s', $this->field, $this->table,$this->where,$this->limit);return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);}//执行新增public function insert(){$sql = sprintf('INSERT %s SET %s ', $this->table,$this->set);return $this->db->query($sql)->rowCount();}//执行编辑public function update(){$sql = sprintf('UPDATE %s SET %s WHERE %s', $this->table,$this->set,$this->where);return $this->db->query($sql)->rowCount();}//执行删除public function delete(){$sql = sprintf('DELETE FROM %s WHERE %s', $this->table,$this->where);return $this->db->query($sql)->rowCount();}}//数据库执行类class DB {//静态委托方法public static function __callStatic($name, $arg){$dsn = 'mysql:host=localhost;dbname=my_user';$username = 'root';$password = 'root123';$query = new Query($dsn, $username, $password);return call_user_func([$query, $name], ...$arg);}}//查询结果$res_se = DB::table('mu_user')->field('*')->where("id<5")->limit('2')->select();var_dump($res_se);echo "<hr>";//新增结果$setstring = "username='狙击手',password='".md5(123456)."',phone='9999999999' ";$res_in = DB::table('mu_user')->set($setstring )->insert();echo "当前新增:".$res_in."条";echo "<hr>";//编辑结果$upstring = "username='观察手',password='".md5(123456)."',phone='88888888' ";$res_up = DB::table('mu_user')->set($upstring )->where("id=17")->update();echo "当前编辑:".$res_up."条";echo "<hr>";//删除结果$res_de = DB::table('mu_user')->where("id=18")->delete();echo "当前删除:".$res_de."条";
2.执行后
3.执行前数据库截图

3.执行后数据库截图

__get(),__set(),__isset(), __unset()及构造方法__construct()1.代码部分:
<?phpclass Goods{private $product;private $price;private $payment;private $press= '';//构造方法:属于魔术方式,是一种公共方法public function __construct($product,$price){$this->product = $product;$this->price = $price;}// __get($name): 当外部访问一个不存在或者无权限访问的属性的时候会自动调用//__get():用于访问调用public function __get($name){$fun = "get".ucfirst($name);//ucfirst:转首字母大写// method_exists (对象示例或者类名, 方法名):检查类的方法是否存在于指定的object中return method_exists($this,$fun) ? $this->$fun() : "暂无数据";}private function getInfo(){$this->payment = $this->price*0.8;return mb_substr($this->product,0,6)." , 原价:".$this->price." , 实付:".$this->payment."元";}// __set():可以将用户对属性的更新操作进行重定向,可以用于属性设置public function __set($name, $value){$method = 'set'.ucfirst($name);return method_exists($this,$method) ? $this->$method($value) : $value;}//属性编辑private function setproduct($value){return $this->product = "<".$value.">";}private function setPrice($value){return $this->price = $value;}//__isset($name):当类中成员变量(属性)不能外部访问,会自动调用 ,这样可以直接在魔术方法中判断成员变量是否存在,//当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。public function __isset($name){return isset($this->$name) ;}// __unset():删除成员变量(属性)会调用执行删除操作public function __unset($name){unset($this->$name);}}$goods = new Goods("钢铁是怎么样炼成的",60);echo $goods->info;echo "<hr>";//属性设置$goods ->product = "百年孤独";$goods ->price = 100;//访问echo $goods->info;echo "<hr>";echo (isset($goods->press)==1) ? "存在" : "不存在";echo "<hr>";unset($goods->press);echo (isset($goods->press)==1) ? "存在" : "不存在";
2.执行后效果图:
代码部分:
<?phpclass amode{public static function index(){return __METHOD__;}public static function info(){return static::index();// return new self;报错// self:总是与当前声明该方法(amode)的类绑定,并不能与调用类(order/order_list)绑定// static: 后期静态绑定,可以自动与当前方法的调用类进行绑定}}echo amode::info();echo "<hr>";class order extends amode{}echo order::info();
效果图
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号