Blogger Information
Blog 31
fans 0
comment 0
visits 18237
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP第18课-类静态后期绑定与MVC-九期线上班
Content っ
Original
701 people have browsed it

1.将课堂源代码, 全部写一遍, 在编辑器中写就可, 必须全部运行正确, 并加上自己理解的注释,不用手写

demo1.php类静态static

  1. <?php
  2. //静态static
  3. namespace _1204;
  4. use PDO;
  5. //声明定义
  6. class Demo1
  7. {
  8. //静态成员
  9. public static $pdo;
  10. public static $dsn = 'mysql:host=localhost;dbname=jason';
  11. public static $username = 'root';
  12. public static $password = 'root';
  13. //静态方法
  14. public static function connect()
  15. {
  16. self::$pdo = new PDO(self::$dsn,self::$username,self::$password);
  17. }
  18. //静态查询
  19. public static function select(){
  20. self::connect();
  21. return self::$pdo->query('select * from `account`',PDO::FETCH_ASSOC);
  22. }
  23. }
  24. //实例化db1
  25. $result = Demo1::select();
  26. foreach ($result as $row) {
  27. echo '<pre>' . print_r($row, true) . '</pre>';
  28. }

demo2.php后期绑定static

  1. <?php
  2. //后期静态绑定,延迟绑定
  3. namespace _1204;
  4. use PDO;
  5. //声明db1类
  6. class Db1{
  7. //创建静态成员
  8. protected static $pdo;
  9. protected static $dsn = 'mysql:host=localhost;dbname=jason';
  10. protected static $username = 'root';
  11. protected static $password = '123456';
  12. //创建静态方法
  13. protected static function connect(){
  14. //static表示谁调用他就是谁
  15. static::$pdo = new PDO(static::$dsn,static::$username,static::$password);
  16. }
  17. //创建静态查询方法
  18. public static function select(){
  19. static::connect();
  20. return static::$pdo->query('select * from `account`',PDO::FETCH_ASSOC);
  21. }
  22. }
  23. //声明定义db1_1类,继承db1类
  24. class Db1_1 extends Db1{
  25. //子类修改静态成员
  26. protected static $username = 'root';
  27. protected static $password = 'root';
  28. //重写父类方法
  29. protected static function connect(){
  30. static::$pdo = new PDO(static::$dsn,static::$username,static::$password);
  31. }
  32. }
  33. //实例化db1_1
  34. $result = Db1_1::select();
  35. foreach ($result as $row){
  36. echo '<pre>' . print_r($row, true) . '</pre>';
  37. }

运行效果

2.MVC

model.php模型

  1. <?php
  2. namespace mvc;
  3. class Model{
  4. public function getData(){
  5. return [
  6. ['id'=>1,'name'=>'苹果电脑','model' => 'MacBook Pro','price'=>25800],
  7. ['id'=>2,'name'=>'华为手机','model' => '华为P30 Pro','price'=>5800],
  8. ['id'=>3,'name'=>'小爱同学','model' => '小爱音响','price'=>800],
  9. ];
  10. }
  11. }

view.php视图

  1. <?php
  2. namespace mvc;
  3. //视图层
  4. class View
  5. {
  6. public function fetch($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption>商品信息表</caption>';
  10. $table .= '<tr><th>ID</th><th>品名</th><th>型号</th><th>价格</th></tr>';
  11. foreach ($data as $product) {
  12. $table .= '<tr>';
  13. $table .= '<td>' . $product['id'] . '</td>';
  14. $table .= '<td>' . $product['name'] . '</td>';
  15. $table .= '<td>' . $product['model'] . '</td>';
  16. $table .= '<td>' . $product['price'] . '</td>';
  17. $table .= '</tr>';
  18. }
  19. $table .= '</table>';
  20. return $table;
  21. }
  22. }
  23. echo '<style>
  24. table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
  25. caption {font-size: 1.2rem; margin-bottom: 10px;}
  26. tr:first-of-type { background-color:lightblue;}
  27. td,th {border: 1px solid}
  28. td:first-of-type {text-align: center}
  29. </style>';

controller.php控制器

  1. <?php
  2. namespace mvc;
  3. //引入视图和模型
  4. require '../Models/model.php';
  5. require '../Views/view.php';
  6. //定义控制器类
  7. class Controller1{
  8. public function index(){
  9. //创建模型类
  10. $model = new Model();
  11. //获取模型中的二维数组
  12. $data = $model->getData();
  13. //创建视图类
  14. $view = new View();
  15. //调用视图类并将模型给视图
  16. return $view->fetch($data);
  17. }
  18. }
  19. //实例化对象
  20. $c1 = new Controller1();
  21. echo $c1->index();

demo2.php通过注入方式降低耦合

  1. <?php
  2. namespace mvc;
  3. //引入视图模型类
  4. require '../Views/view.php';
  5. require '../Models/model.php';
  6. class Controller2{
  7. //外部引入降低耦合
  8. public function index(Model $model,View $view){
  9. //调用视图给数据
  10. return $view->fetch($model->getData());
  11. }
  12. }
  13. //实例化控制器
  14. $c2 = new Controller2();
  15. //实例化模型
  16. $model = new Model();
  17. //实例化视图
  18. $view = new View();
  19. echo $c2->index($model,$view);

demo3.php通过构造方法注入方式降低耦合

  1. <?php
  2. //构造函数依赖绑定
  3. namespace mvc;
  4. //引入模型
  5. require '../Models/model.php';
  6. //引入视图
  7. require '../Views/view.php';
  8. //声明视图
  9. class Controller3{
  10. //定义模型视图成员
  11. protected $model;
  12. protected $view;
  13. //构造函数
  14. public function __construct(Model $model,View $view)
  15. {
  16. $this->view = $view;
  17. $this->model = $model;
  18. }
  19. public function index(){
  20. return $this->view->fetch($this->model->getData());
  21. }
  22. }
  23. //实例化视图模型
  24. $model = new Model();
  25. $view = new View();
  26. //实例化控制器
  27. $c3 = new Controller3($model,$view);
  28. echo $c3->index();

demo4.php通过容器类方式降低耦合

  1. <?php
  2. namespace mvc;
  3. require '../Models/model.php';
  4. require '../Views/view.php';
  5. //创建容器类
  6. class Container{
  7. //定义容器对象
  8. protected $instance = [];
  9. public function bind($alias,\Closure $process){
  10. $this->instance[$alias] = $process;
  11. }
  12. //执行函数
  13. public function make($alias,$params=[]){
  14. //在函数中注册有多个回调内容
  15. return call_user_func_array($this->instance[$alias],[]);
  16. }
  17. }
  18. //创建容器类
  19. $container = new Container();
  20. //加入执行函数绑定
  21. $container->bind('model',function (){return new Model();});
  22. $container->bind('view',function (){return new View();});
  23. //创建控制器类
  24. class Controller4{
  25. public function index(Container $container){
  26. //取出模型
  27. $data = $container->make('model')->getData();
  28. //取出视图并且赋值模型
  29. return $container->make('view')->fetch($data);
  30. }
  31. }
  32. $c4 = new Controller4();
  33. echo $c4->index($container);

demo5.php通过容器类 Facade门面类方式降低耦合

  1. <?php
  2. namespace mvc;
  3. require '../Views/view.php';
  4. require '../Models/model.php';
  5. //创建服务器容器类
  6. class Container1{
  7. protected $instance = [];
  8. //放入容器
  9. public function bind($alias,\Closure $closure){
  10. $this->instance[$alias] = $closure;
  11. }
  12. //取出
  13. public function make($alias,$params=[]){
  14. return call_user_func_array($this->instance[$alias],$params);
  15. }
  16. }
  17. //实例化容器类
  18. $container1 = new Container1();
  19. $container1->bind('model',function (){return new Model();});
  20. $container1->bind('view',function (){return new View();});
  21. //创建facade门面类
  22. class Facade{
  23. //创建容器成员
  24. protected static $container ;
  25. //data成员
  26. protected static $data = [];
  27. //初始化方法
  28. public static function init(Container1 $container1){
  29. static::$container = $container1;
  30. }
  31. //获取data方法
  32. public static function getData(){
  33. static::$data = static::$container->make('model')->getData();
  34. }
  35. //显示view
  36. public static function fetch(){
  37. return static::$container->make('view')->fetch(static::$data);
  38. }
  39. }
  40. //控制器类
  41. class Controller5{
  42. //构造函数
  43. public function __construct(Container1 $container1)
  44. {
  45. Facade::init($container1);
  46. }
  47. public function index(){
  48. //获取数据
  49. Facade::getData();
  50. //显示数据
  51. return Facade::fetch();
  52. }
  53. }
  54. //实例化控制器类
  55. $c5 = new Controller5($container1);
  56. echo $c5->index();

2.1运行效果

2.2将最后一个demo5.php中的代码, 手写提交

总结

  1. 今天学习了类的静态成员方法和延迟绑定的静态,静态成员和方法不需要通过类的实例化来调用,直接(类名::方法名或者成员)即可调用,后期绑定用(static::)是代码更加通用,这样如果是子类重写父类方法时候,就会优先调用本类的;
  2. 然后学习了MVC,M:model模型,负责数据方面的事情,V:view视图:负责显示的事情,C:controll控制器,负责协调模型和视图的控制器,注意,视图和模型不能直接交互,只能通过控制器协调,这就是为了降低视图模型和控制器之间的耦合度,然后学习了通过容器和门面类来降低耦合度,第一遍直播看的很晕,在看了一遍回放后,大致明白这样写的好处,主要是为了降低耦合,让程序更加的通用健壮。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:mvc流程都明白, 后面就好办了
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