找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 957|回复: 0

继承

[复制链接]

304

主题

42

回帖

1338

积分

管理员

积分
1338
发表于 2021-4-22 12:20:37 | 显示全部楼层 |阅读模式
继承
  1. <?php
  2. /*
  3.         继承
  4.                 直观目标:
  5.                         解决代码的重用

  6.                 实现步骤:
  7.                         1. 提取重复的代码
  8.                         2. 建立联系, extends
  9. */

  10. // 父类 - 基类
  11. class Animal
  12. {
  13.         public $money = 100;

  14.         public function eat()
  15.         {
  16.                 echo "{$this->name} 开始吃饭饭!<br>";
  17.         }

  18.         public function drink()
  19.         {
  20.                 echo "{$this->name} 开始喝水水!<br>";
  21.         }
  22. }

  23. // 子类 - 派生类 - 继承者
  24. class Dog extends Animal
  25. {
  26.         public $name = '';
  27.         public function __construct($name)
  28.         {
  29.                 $this->name = $name;
  30.         }
  31. }

  32. class Cat extends Animal
  33. {
  34.         public $name = '';
  35.         public function __construct($name)
  36.         {
  37.                 $this->name = $name;
  38.         }
  39. }

  40. class Pig extends Animal
  41. {
  42.         public $name = '';
  43.         public function __construct($name)
  44.         {
  45.                 $this->name = $name;
  46.         }
  47. }

  48. $dog = new Dog('旺财');
  49. $cat = new Cat('咖啡猫');
  50. $pig = new Pig('佩奇');

  51. $dog->eat();
  52. $cat->eat();
  53. $pig->eat();

  54. $dog->drink();
  55. $cat->drink();
  56. $pig->drink();

  57. $dog->money = $dog->money - 2;

  58. echo '<pre>';
  59.         print_r($dog);
  60. echo '</pre>';

  61. echo '<pre>';
  62.         print_r($cat);
  63. echo '</pre>';

  64. echo '<pre>';
  65.         print_r($pig);
  66. echo '</pre>';

  67. // 使用了继承,父类拥有的所有属性和方法,子类都有。
复制代码
mysqli
  1. <?php
  2. define('DB_HOST', 'localhost');
  3. define('DB_USER', 'root');
  4. define('DB_PASSWD', 'jiege');
  5. define('DB_NAME', 'test');
  6. define('DB_PORT', '3306');

  7. // $mysql = new MySQLi(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);

  8. // // 组装SQL
  9. // $sql = 'select * from user limit 1';

  10. // // 发送执行
  11. // $result = $mysql->query($sql);

  12. // // 检索结果
  13. // $rows = $result->fetch_all(MYSQLI_ASSOC);

  14. // echo '<pre>';
  15. //         print_r($rows);
  16. // echo '</pre>';


  17. class Model
  18. {
  19.         public $mysql = null;
  20.         public $result = null;

  21.         public function __construct()
  22.         {
  23.                 // mysql 的连接
  24.                 $this->mysql = new MySQLi(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
  25.         }

  26.         public function find(int $id)
  27.         {
  28.                 // 组装SQL
  29.                 $sql = "select * from `{$this->table}` where `id` = {$id} limit 1";

  30.                 echo $sql . '<br>';

  31.                 $this->query($sql);
  32.                 return $this->fetch();
  33.         }

  34.         public function query($sql)
  35.         {
  36.                 // 发送执行
  37.                 $this->result = $this->mysql->query($sql);
  38.         }

  39.         public function fetch() : array
  40.         {
  41.                 // 检索结果
  42.                 $rows = $this->result->fetch_all(MYSQLI_ASSOC);

  43.                 return $rows;
  44.         }
  45. }

  46. class User extends Model
  47. {
  48.         public $table = 'user';
  49. }

  50. $user = new User();
  51. $list = $user->find(1);
  52. echo '<pre>';
  53.         print_r($list);
  54. echo '</pre>';

  55. class Products extends Model
  56. {
  57.         public $table = 'products';
  58. }
  59. $product = new Products();
  60. $list = $product->find(2);
  61. echo '<pre>';
  62.         print_r($list);
  63. echo '</pre>';
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇论坛 ( 粤ICP备16021788号 )

GMT+8, 2024-10-23 07:27 , Processed in 0.069490 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表