Blogger Information
Blog 26
fans 1
comment 0
visits 22357
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP第课:1. 抽象类 2. 接口 3、Db封装 11月29日
孤忆寻昔R
Original
764 people have browsed it

1、抽象类

  1. <?php
  2. //抽象类
  3. abstract class a
  4. {
  5. public $name;
  6. public function __construct($name)
  7. {
  8. $this->name = $name;
  9. }
  10. public function af()
  11. {
  12. echo $this->name.'111';
  13. }
  14. // 这个方法是抽象方法
  15. abstract public function aff();
  16. }
  17. class b extends a
  18. {
  19. //必须重写这个方法
  20. public function aff()
  21. {
  22. echo $this->name.'222';
  23. }
  24. }
  25. $b = new b('欧阳克');
  26. echo $b->aff();
  27. abstract class Person
  28. {
  29. protected $name;
  30. protected function __construct($name)
  31. {
  32. $this->name =$name;
  33. }
  34. public function getName($name='欧阳克')
  35. {
  36. return $this->name;
  37. }
  38. abstract protected function setName($v);
  39. }
  40. class Stu extends Person {
  41. public function __construct($name='欧阳克')
  42. {
  43. parent::__construct($name);
  44. }
  45. public function setName($v)
  46. {
  47. $this->name = $v;
  48. }
  49. }
  50. $s = new Stu('诸葛亮');
  51. //echo 'PHP中文网创始人'.$s->getName();
  52. $s -> setName('朱老师');
  53. echo 'php中文网前段讲师:'. $s->getName();

2、接口

  1. <?php
  2. //接口
  3. //必须全部是抽象方法
  4. interface V
  5. {
  6. const LXR = '欧阳克';
  7. public function setFuel($fuel);
  8. public function setPurpose($purpose);
  9. }
  10. abstract class Auro implements V{
  11. public $fuel;
  12. public function setFuel($fuel)
  13. {
  14. $this->fuel = $fuel;
  15. }
  16. }
  17. class Car implements V{
  18. public $fuel;
  19. public $purpose;
  20. public function __construct($purpose,$fuel)
  21. {
  22. $this->purpose =$purpose;
  23. $this->fuel =$fuel;
  24. }
  25. public function setFuel($fuel)
  26. {
  27. $this->fuel = $fuel;
  28. }
  29. public function setPurpose($purpose)
  30. {
  31. $this->purpose = $purpose;
  32. }
  33. public function getInfo()
  34. {
  35. return $this->fuel.$this->purpose;
  36. }
  37. }
  38. $ks = new Car('电动车','公交');
  39. echo $ks ->getInfo('zhu');

3、上次的作业继续继续完成 封装Db类

  1. <?php
  2. //公共方法库
  3. class Db
  4. {
  5. protected $table = null;
  6. protected $pdo;
  7. public function __construct($dsn,$user,$password,$table)
  8. {
  9. $this->pdo = new PDO($dsn,$user,$password);
  10. $this->table = $table;
  11. }
  12. // 查询方法
  13. //select * from biao WHERE 条件 order by 排序 limit 页
  14. public function select($where='',$order='',$limilt='0,10', $filed='*')
  15. {
  16. //select from 必须有
  17. $sql = 'SELECT ';
  18. if($filed == '*'){
  19. $sql .= '* FROM ';
  20. } else{
  21. $sql .= $filed.' FROM ';
  22. }
  23. $sql .= $this->table;
  24. if(!$where){
  25. $sql .= ' WHERE '.$where;
  26. }
  27. print_r($sql);
  28. if($order)
  29. {
  30. $sql .= ' ORDER BY '.$order;
  31. }
  32. print_r($sql);
  33. $stmt = $this->pdo->prepare($sql);
  34. if($stmt->execute())
  35. {
  36. if($stmt->rowCount()>0){
  37. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  38. return $stmt->fetchAll();
  39. }
  40. }else {
  41. return false;
  42. }
  43. $sql = 'SELECT FROM';
  44. }
  45. //插入数据
  46. public function insert($table,$data)
  47. {
  48. $sql = "INSERT INTO `{$table}` SET ";
  49. //组装插入语句
  50. if(is_array($data)){
  51. foreach ($data as $k=>$v) {
  52. $sql .= $k.'="'.$v.'", ';
  53. }
  54. }else{
  55. return false;
  56. }
  57. // print_r($sql);
  58. //去掉尾部逗号,并添加分号结束
  59. $sql = rtrim(trim($sql),',').';';
  60. //创建PDO预处理对象
  61. // print_r($sql);
  62. $stmt = $this->pdo->prepare($sql);
  63. //执行新增操作
  64. // print_r($stmt);
  65. if($stmt->execute()){
  66. if($stmt->rowCount()>0){
  67. $num = $stmt->rowCount();
  68. if($num){
  69. $id = $this->pdo->lastInsertId();
  70. echo "<h3>成功新增 {$num}了条记录.新增记录的主键id:{$id}</h3>";
  71. }
  72. }
  73. } else {
  74. return false;
  75. }
  76. // print_r($stmt);
  77. }
  78. // 更新操作
  79. function update($table,$data,$where='') {
  80. //创建SQL语句
  81. $sql = "UPDATE `{$table}` SET ";
  82. //组装修改语句
  83. if(is_array($data)){
  84. foreach ($data as $k=>$v) {
  85. $sql .= $k.'="'.$v.'", ';
  86. }
  87. }
  88. // print_r($sql);
  89. //去掉尾部逗号,并添加分号结束
  90. $sql = rtrim(trim($sql),',');
  91. //查询条件
  92. if(!empty($where)){
  93. $sql .= ' WHERE '.$where;
  94. }
  95. // print_r($sql);
  96. //创建PDO预处理对象
  97. $stmt = $this->pdo->prepare($sql);
  98. //执行新增操作
  99. if($stmt->execute()){
  100. if($stmt->rowCount()>0){
  101. $num = $stmt->rowCount();
  102. if($num){
  103. $id = $this->pdo->lastInsertId();
  104. echo "<h3>成功更新 {$num}了条记录</h3>";
  105. }
  106. }
  107. } else {
  108. return false;
  109. }
  110. }
  111. public function delete($table,$where){
  112. // 预处理执行删除操作
  113. $sql = 'DELETE FROM '.$table.' WHERE '.$where;
  114. $stmt = $this->pdo->prepare($sql);
  115. $stmt->execute();
  116. return $stmt->rowCount();
  117. }
  118. }
  119. //实例化对象 并向 构造方法传参
  120. $a = new Db('mysql:host=127.0.0.1;dbname=pdo','root','root','users');
  121. ////查询方法 查询多个记录
  122. //$selects = $a->select('id=4 AND id = 11','age DESC','0,10','id,username,password');
  123. //
  124. //print_r($selects);
  125. ////插入数据 单条
  126. //$insert = $a->insert('users',['username'=>'张三','password'=>sha1('123456'),'age'=>35]);
  127. //print_r($insert);
  128. //
  129. //$update = $a ->update('users',['username'=>'老黄','password'=>sha1(123456),'age'=>'18'],"id=153");
  130. //print_r($update);
  131. // 删除记录
  132. $where = 'id >= 150';
  133. echo '成功删除了: ' .$a->delete('users',$where). ' 条记录';
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:作业写得有点晚了, 不过写了就好
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post