批改状态:合格
老师批语:其实掌握这些还是有点难度的, 辛苦
<?php//创建一个数据库连接的类class DB{protected $dbConn;private $paras = [];public function __construct(){$this ->paras['host'] = 'localhost';$this ->paras['userName'] = 'root';$this ->paras['passWord'] = 'root';$this ->paras['dbName'] = 'db_pursey';$this ->conn();}public function conn(){$this ->dbConn = mysqli_connect(...array_values($this->paras));mysqli_query($this->dbConn,'set names utf8');}public function select($table,$fields,$where,$limit){$sqlStr = "select $fields from `$table` where $where limit $limit";$res = mysqli_query($this->dbConn,$sqlStr);//把查询结果放在一个二维数组中foreach($res as $info):$rows[]=$info;endforeach;//返回二维数据return $rows;}public function printInfo($res){foreach($res as $row):echo '类型:',$row['type'],'<br>';echo '标题:',$row['title'],'<br>';echo '内容:',mb_substr($row['content'],0,10),'...','<br>';echo '联系人:',$row['linkman'],'<br>';echo '电话:',$row['tel'],'<br>';echo '发布日期:',$row['edate'],'<br>';echo '<hr>';endforeach;}//类的对象被序列化的时候触发该方法public function __sleep(){//把数据库连接参数在序列化时返回return ['paras'];}//反序列化 (一个被序列化的该类的对象) 时触发该方法public function __wakeup(){//反序列号时自动连接数据库$this ->conn();}public function __clone(){echo '**********克隆了一个新的数据库连接对象********';echo '<hr>';}}//普通方法连接数据库$db = new DB();$res = $db -> select('tb_info','*','type="公寓信息"',1);$db ->printInfo($res);//把对象中的数据库连接参数序列化到一个字符串后保存在文件中$str = serialize($db);file_put_contents('conn.txt',$str);//从文件中取出连接参数$fileStr = file_get_contents('conn.txt');//反序列化后生成一个数据库连接对象$db1 = unserialize($fileStr);//生成一个新的数据库连接对象$res = $db1 -> select('tb_info','*','type="公寓信息"',2);$db1 ->printInfo($res);//克隆一个全新的对象//如果写成$db2 = $db 只是给db起了一个别名db2$db2 = clone $db; //克隆一个新对象时,触发__clone()方法$res = $db2 -> select('tb_info','*','type="公寓信息"',3);$db2 ->printInfo($res);?>

<?php//自定义一个异常类,直接返回抛出的异常字符串class ExCheckUser extends Exception{public function __toString(){return $this->message;}}//新建一个用户类,包括用户名和密码class User{private $userName;private $passWord;public function __construct($userName,$passWord){$this->userName = $userName;$this->passWord = $passWord;}public function checkUser(){try{if($this ->userName === 'angle'):if($this ->passWord === '123'):echo $this ->userName,'登录成功!!!','<br>';else:throw new ExCheckUser('密码输入错误');//抛出异常后,调有异常类后执行catch中的语句endif;else:throw new ExCheckUser('用户名输入错误');endif;}catch(ExCheckUser $e){echo $e,'--------------',$this ->userName,'登录失败!!!','<br>';}}}$user = new User('angle','123');$user->checkUser();$user = new User('angle','123456');$user->checkUser();$user = new User('hugn','123');$user->checkUser();?>

<?php//匿名类//1.用在这个类只是临时调用一次$user = new class('angle','123')//创建一个匿名类,构造方法传参{private $name;private $passWord;public function __construct($name,$passWord){$this->name = $name;$this->passWord = $passWord;}public function checkUser(){if (($this->name === 'angle')&&($this->passWord ==='123')):return $this->name.'登录成功';else:return $this->name.'登录失败';endif;}};echo $user ->checkUser();//调用对象中的检测方法echo '<hr>';//2.用在函数参数是对象时function printInfo(object $user){//如果echo一个对象,会触发类的tostring方法echo $user;}echo printInfo(new class{public function __toString(){return 'hello 我是一个类';}})?>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号