批改状态:合格
老师批语:拦截器是属性重载的应用场景之一, 并不是全部,其它场景要在实战中慢慢体会
<?php//后期静态绑定:就是在调用静态方法时,动态绑定到调用它的类上,而不是定义化的类abstract class School{public static function printSDC(){$sdcInfo = new static();//创建一个与当前调用类绑定的实例echo '班名:',$sdcInfo->classes,'<br>';echo '系名:',$sdcInfo->department,'<br>';echo '校名:',$sdcInfo->school,'<br>';echo '<hr>';}}class Qhdx extends School{protected $school;protected $department;protected $classes;public function __construct(){$this->school = '清华大学';$this->department ='计算机系';$this->classes = '04-01';}}class Rmdx extends School{protected $school;protected $department;protected $classes;public function __construct(){$this->school = '人民大学';$this->department ='工商管理系';$this->classes = '08-03';}}Qhdx::printSDC();Rmdx::printSDC();

<?php//类中的构造方法,在实例化的时候调用class School{private $school;private $department;private $classes;//在类被实例化时自动触发public function __construct(){$this->school = '清华大学';$this->department ='计算机系';$this->classes = '04-01';}public function printSDC(){echo '班名:',$this->classes,'<br>';echo '系名:',$this->department,'<br>';echo '校名:',$this->school,'<br>';echo '<hr>';}public static function test1(){echo __METHOD__,'静态方法存在','<br>';}//类中的属性拦截器//访问的类属性 不存在或者没有访问权限时触发此函数public function __get($name){if(isset($this->$name)):echo '没有权限访问',$name,'属性','<br>';else:echo '不存在',$name,'属性','<br>';endif;}//设置的类属性 不存在或者没有访问权限时触发此函数public function __set($name, $value){if($name === 'school'):echo '不能修改学校名称','<br>';else:$this ->$name = $value;endif;}//检测的类属性 不存在或者没有访问权限时触发此函数public function __isset($name){if(isset($this->$name)):echo '没有权限检测',$name,'属性','<br>';else:echo '不存在',$name,'属性','<br>';endif;}//删除的类属性 不存在或者没有访问权限时触发此函数public function __unset($name){if(isset($this->$name)):echo '没有权限访问',$name,'属性','<br>';else:echo '不存在',$name,'属性','<br>';endif;}//类中的方法拦截器//调用类的普通方法 不存在或者没有访问权限时触发此函数public function __call($name, $arguments){echo '您访问的',__CLASS__,'::',$name,'方法不存在','<br>';}//调用类的静态方法 不存在或者没有访问权限时触发此函数public static function __callStatic($name, $arguments){echo '您访问的',__CLASS__,'::',$name,'静态方法不存在','<br>';}}$school = new School();$school -> printSDC();//因为使用了属性拦截器所以类外调用时不会报错echo $school ->school;//访问存在的私有属性echo $school ->school1;//访问不存在的私有属性$school ->school = '人民大学';//属性设置拦截器中限制不能修改学校名称$school ->department = '工商管理系';//属性设置拦截器中进行设置$school -> printSDC();isset($school->school); //因为school是私有属性,所以检测它是触发了__isset()方法isset($school->school1);$school -> printSDC();$school -> printSDC1();//printSDC1并没有在类中定义,所以触发了__call()方法School::test1();//访问一个存在的静态方法School::test2();//访问一个不存在的静态方法 触发了静态方法__callStatic()?>

<?php//连接mysql服务器//创建一个抽象类,用来连接数据库abstract class Conn{protected $host= 'localhost';protected $userName= 'root';protected $passWord= 'root';protected $dbName;protected $conn;public function __construct($dbName){//构造函数中传入数据库的名称$this ->dbName = $dbName;//构造方法中连接数据库$this ->connect();}//数据库连接函数public function connect(){$host = $this ->host;$userName = $this ->userName;$passWord = $this ->passWord;$dbName = $this ->dbName;$this ->conn = mysqli_connect($host,$userName,$passWord,$dbName);//设置数据库连接的查询字符集,为utf8mysqli_query($this ->conn,'set names utf8');}}class Query extends Conn{protected $table;protected $field;protected $where;protected $limit;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 limit($limit){$this ->limit = $limit;return $this;}public function getSql(){//生成查询字符串return sprintf('select %s from `%s` where %s limit %s',$this->field,$this->table,$this->where,$this->limit);}public function select(){//在数据库中查询数据$res = mysqli_query($this->conn,$this->getsql());//把查询结果放在一个二维数组中foreach($res as $info):$rows[]=$info;endforeach;//返回二维数据return $rows;}}class DB{//使用静态方法拦截器public static function __callStatic($name, $arguments){//为db_pursey数据库创建一个连接$conn = new Query('db_pursey');//call_user_func把第一个参数做为回调函数调用,其于为回调函数的参数return call_user_func([$conn,$name],...$arguments);}}//DB类中没有定义静态方法table(),触发静态方法拦截器__callStatic;$res = DB::table('tb_info')->field('*')->where('type = "公寓信息"')->limit(3)->select();foreach($res as $row):echo '类型:',$row['type'],'<br>';echo '标题:',$row['title'],'<br>';echo '内容:',$row['content'],'<br>';echo '联系人:',$row['linkman'],'<br>';echo '电话:',$row['tel'],'<br>';echo '发布日期:',$row['edate'],'<br>';echo '<hr>';endforeach;?>

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