博客列表 >PHP 后期静态绑定,构造函数,属性和方法拦截器

PHP 后期静态绑定,构造函数,属性和方法拦截器

王娇
王娇原创
2020年05月04日 00:31:06861浏览

学习总结

  • 后期静态绑定可以把定义的静态方法与调用它的类进行绑定,而不是定义它的类
  • 构造方法是在类被实例化的时候由系统自动执行的
  • 属性拦截器可以在属性被访问,设置,检测,删除时判断,如果没有权限或者不存在该属性则触发对应的方法。
  • 方法和静态方法拦截器是在方法被访问时,如果不存在在或者没有权限访问,则触发对应的方法。

1.后期静态绑定

  1. <?php
  2. //后期静态绑定:就是在调用静态方法时,动态绑定到调用它的类上,而不是定义化的类
  3. abstract class School
  4. {
  5. public static function printSDC(){
  6. $sdcInfo = new static();//创建一个与当前调用类绑定的实例
  7. echo '班名:',$sdcInfo->classes,'<br>';
  8. echo '系名:',$sdcInfo->department,'<br>';
  9. echo '校名:',$sdcInfo->school,'<br>';
  10. echo '<hr>';
  11. }
  12. }
  13. class Qhdx extends School
  14. {
  15. protected $school;
  16. protected $department;
  17. protected $classes;
  18. public function __construct()
  19. {
  20. $this->school = '清华大学';
  21. $this->department ='计算机系';
  22. $this->classes = '04-01';
  23. }
  24. }
  25. class Rmdx extends School
  26. {
  27. protected $school;
  28. protected $department;
  29. protected $classes;
  30. public function __construct()
  31. {
  32. $this->school = '人民大学';
  33. $this->department ='工商管理系';
  34. $this->classes = '08-03';
  35. }
  36. }
  37. Qhdx::printSDC();
  38. Rmdx::printSDC();
  • 运行效果图

2.构造函数和属性方法拦截器

  1. <?php
  2. //类中的构造方法,在实例化的时候调用
  3. class School
  4. {
  5. private $school;
  6. private $department;
  7. private $classes;
  8. //在类被实例化时自动触发
  9. public function __construct()
  10. {
  11. $this->school = '清华大学';
  12. $this->department ='计算机系';
  13. $this->classes = '04-01';
  14. }
  15. public function printSDC()
  16. {
  17. echo '班名:',$this->classes,'<br>';
  18. echo '系名:',$this->department,'<br>';
  19. echo '校名:',$this->school,'<br>';
  20. echo '<hr>';
  21. }
  22. public static function test1()
  23. {
  24. echo __METHOD__,'静态方法存在','<br>';
  25. }
  26. //类中的属性拦截器
  27. //访问的类属性 不存在或者没有访问权限时触发此函数
  28. public function __get($name)
  29. {
  30. if(isset($this->$name)):
  31. echo '没有权限访问',$name,'属性','<br>';
  32. else:
  33. echo '不存在',$name,'属性','<br>';
  34. endif;
  35. }
  36. //设置的类属性 不存在或者没有访问权限时触发此函数
  37. public function __set($name, $value)
  38. {
  39. if($name === 'school'):
  40. echo '不能修改学校名称','<br>';
  41. else:
  42. $this ->$name = $value;
  43. endif;
  44. }
  45. //检测的类属性 不存在或者没有访问权限时触发此函数
  46. public function __isset($name)
  47. {
  48. if(isset($this->$name)):
  49. echo '没有权限检测',$name,'属性','<br>';
  50. else:
  51. echo '不存在',$name,'属性','<br>';
  52. endif;
  53. }
  54. //删除的类属性 不存在或者没有访问权限时触发此函数
  55. public function __unset($name)
  56. {
  57. if(isset($this->$name)):
  58. echo '没有权限访问',$name,'属性','<br>';
  59. else:
  60. echo '不存在',$name,'属性','<br>';
  61. endif;
  62. }
  63. //类中的方法拦截器
  64. //调用类的普通方法 不存在或者没有访问权限时触发此函数
  65. public function __call($name, $arguments)
  66. {
  67. echo '您访问的',__CLASS__,'::',$name,'方法不存在','<br>';
  68. }
  69. //调用类的静态方法 不存在或者没有访问权限时触发此函数
  70. public static function __callStatic($name, $arguments)
  71. {
  72. echo '您访问的',__CLASS__,'::',$name,'静态方法不存在','<br>';
  73. }
  74. }
  75. $school = new School();
  76. $school -> printSDC();
  77. //因为使用了属性拦截器所以类外调用时不会报错
  78. echo $school ->school;//访问存在的私有属性
  79. echo $school ->school1;//访问不存在的私有属性
  80. $school ->school = '人民大学';//属性设置拦截器中限制不能修改学校名称
  81. $school ->department = '工商管理系';//属性设置拦截器中进行设置
  82. $school -> printSDC();
  83. isset($school->school); //因为school是私有属性,所以检测它是触发了__isset()方法
  84. isset($school->school1);
  85. $school -> printSDC();
  86. $school -> printSDC1();//printSDC1并没有在类中定义,所以触发了__call()方法
  87. School::test1();//访问一个存在的静态方法
  88. School::test2();//访问一个不存在的静态方法 触发了静态方法__callStatic()
  89. ?>
  • 运行效果图

3.数据查询构造器实例

  1. <?php
  2. //连接mysql服务器
  3. //创建一个抽象类,用来连接数据库
  4. abstract class Conn
  5. {
  6. protected $host= 'localhost';
  7. protected $userName= 'root';
  8. protected $passWord= 'root';
  9. protected $dbName;
  10. protected $conn;
  11. public function __construct($dbName)
  12. {
  13. //构造函数中传入数据库的名称
  14. $this ->dbName = $dbName;
  15. //构造方法中连接数据库
  16. $this ->connect();
  17. }
  18. //数据库连接函数
  19. public function connect()
  20. {
  21. $host = $this ->host;
  22. $userName = $this ->userName;
  23. $passWord = $this ->passWord;
  24. $dbName = $this ->dbName;
  25. $this ->conn = mysqli_connect($host,$userName,$passWord,$dbName);
  26. //设置数据库连接的查询字符集,为utf8
  27. mysqli_query($this ->conn,'set names utf8');
  28. }
  29. }
  30. class Query extends Conn
  31. {
  32. protected $table;
  33. protected $field;
  34. protected $where;
  35. protected $limit;
  36. public function table($table)
  37. {
  38. $this ->table =$table;
  39. return $this;//返回一个对象
  40. }
  41. public function field($field)
  42. {
  43. $this ->field =$field;
  44. return $this;
  45. }
  46. public function where($where)
  47. {
  48. $this ->where =$where;
  49. return $this;
  50. }
  51. public function limit($limit)
  52. {
  53. $this ->limit = $limit;
  54. return $this;
  55. }
  56. public function getSql()
  57. {
  58. //生成查询字符串
  59. return sprintf('select %s from `%s` where %s limit %s',$this->field,$this->table,$this->where,$this->limit);
  60. }
  61. public function select()
  62. {
  63. //在数据库中查询数据
  64. $res = mysqli_query($this->conn,$this->getsql());
  65. //把查询结果放在一个二维数组中
  66. foreach($res as $info):
  67. $rows[]=$info;
  68. endforeach;
  69. //返回二维数据
  70. return $rows;
  71. }
  72. }
  73. class DB
  74. {
  75. //使用静态方法拦截器
  76. public static function __callStatic($name, $arguments)
  77. {
  78. //为db_pursey数据库创建一个连接
  79. $conn = new Query('db_pursey');
  80. //call_user_func把第一个参数做为回调函数调用,其于为回调函数的参数
  81. return call_user_func([$conn,$name],...$arguments);
  82. }
  83. }
  84. //DB类中没有定义静态方法table(),触发静态方法拦截器__callStatic;
  85. $res = DB::table('tb_info')->field('*')->where('type = "公寓信息"')->limit(3)->select();
  86. foreach($res as $row):
  87. echo '类型:',$row['type'],'<br>';
  88. echo '标题:',$row['title'],'<br>';
  89. echo '内容:',$row['content'],'<br>';
  90. echo '联系人:',$row['linkman'],'<br>';
  91. echo '电话:',$row['tel'],'<br>';
  92. echo '发布日期:',$row['edate'],'<br>';
  93. echo '<hr>';
  94. endforeach;
  95. ?>
  • 运行效果图
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议