Blogger Information
Blog 27
fans 0
comment 0
visits 17368
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
025-11月29日-PHP第15节-抽象类、接口
冇忉丼
Original
521 people have browsed it

1. 抽象类

抽象类可以有抽象方法,也可以有普通方法

  1. abstract class operate{
  2. public $a;
  3. public $b;
  4. //$a与$b注释掉也没问题
  5. // public $sum;
  6. // public $difference;
  7. public function __construct($a,$b){
  8. $this->a = $a;
  9. $this ->b = $b;
  10. }//如果需要给类的实例最初赋值,必须要用构造方法
  11. abstract public function sum();
  12. abstract public function difference();
  13. }
  14. class operate_detail extends operate{
  15. public function sum(){
  16. return $this->a+$this->b;
  17. }
  18. public function difference(){
  19. return $this->a-$this->b;
  20. }
  21. }
  22. // $o = new operate(100,50);
  23. // Cannot instantiate 无法实例化
  24. $o_d =new operate_detail(100,50);
  25. var_dump($o_d->sum());
  26. echo '<hr>';
  27. echo $o_d->difference();

2. 接口

接口是制定规则,只有抽象函数和常量

  1. interface operate2{
  2. const PRODUCT = '积为:';
  3. const QUOTIENT = '商为:';
  4. public function result($c,$d);
  5. //注意这个地方没有abstract因为interface默认了方法就是抽象的
  6. }
  7. class operate2_detail implements operate2{
  8. public function result($c, $d)
  9. {
  10. $result1 = $c*$d;
  11. $result2 = $c/$d;
  12. return $c.'与'.$d.' '.self::PRODUCT.$result1.' --- '.self::QUOTIENT.$result2;
  13. //常量调用要用self::
  14. // return self::PRODUCT.$result1 . '---' . self::QUOTIENT.$result2; '---'前后用不用空格都一样,''里能体现空格
  15. // TODO: Implement result() method.
  16. }
  17. }
  18. $o_d2 = new operate2_detail();
  19. echo $o_d2->result(100,50);

1,2题运行结果

3. 上次的作业写好

按照接口的方式来写,将其简化为4个操作方法:create(insert)、read(select)、update、delete。

  1. interface curd{
  2. public function creat($data);
  3. public function update($data,$where);
  4. public function read($fields, $where, $order, $limit);
  5. public function delete($where);
  6. }
  7. class DB2 implements curd
  8. {
  9. public $pdo;
  10. public $table;
  11. public function __construct($dsn, $user, $password, $table)
  12. {
  13. $this->table = $table;
  14. $this->pdo = new PDO($dsn, $user, $password);
  15. //已经预处理,后面函数不用添加此语句了
  16. }
  17. public function read($fields, $where = '', $order = '', $limit = '')
  18. {
  19. // $this->table = $table;
  20. $sql = 'SELECT ';
  21. if (is_array($fields)) {
  22. foreach ($fields as $field) {
  23. $sql .= $field . ', ';
  24. }
  25. } else {
  26. $sql .= $fields;
  27. }
  28. $sql = rtrim(trim($sql), ',');
  29. $sql .= ' FROM ' . $this->table;
  30. //查询条件
  31. if (!empty($where)) {
  32. $sql .= ' WHERE ' . $where;
  33. }
  34. //排序条件
  35. if (!empty($order)) {
  36. $sql .= ' order by ' . $order;
  37. }
  38. //分页条件
  39. if (!empty($limit)) {
  40. $sql .= ' limit ' . $limit;
  41. }
  42. $sql .= ';';
  43. //创建PDO预处理对象
  44. $stmt = $this->pdo->prepare($sql);
  45. //执行查询操作
  46. if ($stmt->execute()) {
  47. if ($stmt->rowCount() > 0) {
  48. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  49. //返回一个二维数组
  50. return $stmt->fetchAll();
  51. }
  52. } else {
  53. return false;
  54. }
  55. }
  56. public function creat($data = [])
  57. {
  58. $sql = "INSERT INTO {$this->table} SET ";
  59. //组装插入语句
  60. if (is_array($data)) {
  61. foreach ($data as $k => $v) {
  62. $sql .= $k . '="' . $v . '", ';
  63. }
  64. } else {
  65. return false;
  66. }
  67. //去掉尾部逗号,并添加分号结束
  68. $sql = rtrim(trim($sql), ',') . ';';
  69. //创建PDO预处理对象
  70. $stmt = $this->pdo->prepare($sql);
  71. //执行新增操作
  72. if ($stmt->execute()) {
  73. if ($stmt->rowCount() > 0) {
  74. return true;
  75. }
  76. } else {
  77. return false;
  78. }
  79. }
  80. public function update($data = [], $where = '')
  81. {
  82. $sql = "UPDATE {$this->table} SET ";
  83. //组装修改语句
  84. if (is_array($data)) {
  85. foreach ($data as $k => $v) {
  86. $sql .= $k . '="' . $v . '", ';
  87. }
  88. }
  89. //去掉尾部逗号,并添加分号结束
  90. $sql = rtrim(trim($sql), ',');
  91. //查询条件
  92. if (!empty($where)) {
  93. $sql .= ' WHERE ' . $where;
  94. }
  95. //创建PDO预处理对象
  96. $stmt = $this->pdo->prepare($sql);
  97. //执行新增操作
  98. if ($stmt->execute()) {
  99. if ($stmt->rowCount() > 0) {
  100. return true;
  101. }
  102. } else {
  103. return false;
  104. }
  105. }
  106. public function delete($where = '')
  107. {
  108. $sql = "DELETE FROM {$table} ";
  109. //查询条件
  110. if (!empty($where)) {
  111. $sql .= ' WHERE ' . $where;
  112. }
  113. //创建PDO预处理对象
  114. $stmt = $this->pdo->prepare($sql);
  115. //执行删除操作
  116. if ($stmt->execute()) {
  117. if ($stmt->rowCount() > 0) {
  118. return true;
  119. }
  120. } else {
  121. return false;
  122. }
  123. }
  124. }
  125. $db2 = new DB2('mysql:host=localhost;dbname=anguoguo','root','root','movies');
  126. //连接pdo
  127. print_r($db2);
  128. print_r($db2->read('name','cate_id=2','mov_id DESC')) ;
  129. echo '<hr/>';
  130. $array3 =[
  131. 'mov_id' =>10,
  132. 'name' =>'插入',
  133. 'image'=>'10.jpg',
  134. 'detail'=>'能否插入成功',
  135. 'cate_id'=>'4'
  136. ];
  137. print_r($db2->creat('$array3'));
  138. echo '<hr/>';
  139. $array4 =['image'=>'101.jpg'];
  140. print_r($db2->update($array4,'mov_id=5'));
  141. echo '<hr/>';
  142. print_r( $db2->delete(''));
  143. echo '<hr/>';
  144. $array5 =['image'=>'5.jpg'];
  145. print_r($db2->update($array5,'mov_id=5'));
  146. echo '<hr/>';

结果为:

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