Blogger Information
Blog 34
fans 0
comment 0
visits 22391
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月4日—MVC和框架基础
曾龙宇
Original
489 people have browsed it

静态成员

  1. <?php
  2. namespace _1204;
  3. header('content-type:text/html;charset=utf-8');
  4. use PDO;
  5. //static:声明类中的静态成员
  6. class Db1{
  7. //静态成员包含:属性,成员
  8. protected static $pdo;
  9. protected static $dsn = 'mysql:host=localhost;dbname=demo';
  10. protected static $user = 'root';
  11. protected static $pwd = 'root';
  12. public static function connect(){
  13. //在类中访问当前类的静态成员:self
  14. self::$pdo = new PDO(self::$dsn,self::$user,self::$pwd);
  15. }
  16. //测试方法
  17. public static function select(){
  18. //连接数据库
  19. self::connect();
  20. return self::$pdo->query('select * from `user`',PDO::FETCH_ASSOC);
  21. }
  22. }
  23. //在类外部,直接用类名访问静态成员
  24. $result = Db1::select();
  25. //print_r($result);
  26. foreach ($result as $value) {
  27. echo '<pre>'.print_r($value,true);
  28. }

后期静态绑定

  1. <?php
  2. namespace _1204;
  3. header('content-type:text/html;charset=utf-8');
  4. use PDO;
  5. //static:后期静态绑定
  6. //使用场景:静态继承的上下文环境中
  7. //后期绑定就是在调用类成员时绑定,也叫延迟绑定
  8. //可以拿来替代self关键字
  9. class Db2{
  10. //静态成员包含:属性,成员
  11. protected static $pdo;
  12. protected static $dsn = 'mysql:host=localhost;dbname=demo';
  13. protected static $user = 'root';
  14. protected static $pwd = 'root';
  15. public static function connect(){
  16. //在类中访问当前类的静态成员:self
  17. static::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
  18. }
  19. //测试方法
  20. public static function select(){
  21. //连接数据库
  22. static::connect();
  23. return self::$pdo->query('select * from `user`',PDO::FETCH_ASSOC);
  24. }
  25. }
  26. //创建子类,重新定义connet方法,根据user和pwd的不同值
  27. class Sub extends Db2{
  28. protected static $user = 'zhangsan';
  29. protected static $pwd = '123456';
  30. public static function connect(){
  31. self::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
  32. }
  33. }
  34. //在类外部,直接用类名访问静态成员
  35. //Db2的user和pwd是正确的,所以可以读取数据库数据
  36. $result = Db2::select();
  37. //print_r($result);
  38. foreach ($result as $value) {
  39. echo '<pre>'.print_r($value,true);
  40. }
  41. //sub类重新定义错误的user和pwd值,不能读取数据
  42. $result1 = Sub::select();
  43. print_r($result1);

MVC框架

Model类

  1. <?php
  2. namespace mvc;
  3. header('content-type:text/html;charset=utf-8');
  4. //模型类:用于数据表的操作
  5. use PDO;
  6. class Model{
  7. private static $pdo;
  8. private static $dsn = 'mysql:host=localhost;dbname=demo';
  9. private static $user = 'root';
  10. private static $pwd = 'root';
  11. private static $sql = 'select * from `product`';
  12. public static function connect(){
  13. static::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
  14. }
  15. public function getData(){
  16. static::connect();
  17. return static::$pdo->query(static::$sql,PDO::FETCH_ASSOC);
  18. }
  19. }

View类

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

Controller类

普通控制类
  1. <?php
  2. namespace mvc;
  3. //控制类:向模型类请求读取数据,再发送给视图类渲染出来
  4. //加载要用到的模型类和视图类
  5. require 'Model.php';
  6. require 'View.php';
  7. class Controller1{
  8. public function index(){
  9. //获取模型类的数据
  10. $model = new Model();
  11. $data = $model->getData();
  12. //渲染给视图类
  13. $view = new View();
  14. return $view->fetch($data);
  15. }
  16. }
  17. $controller1 = new Controller1();
  18. echo $controller1->index();
方法传参控制类
  1. <?php
  2. namespace mvc;
  3. //控制类:向模型类请求读取数据,再发送给视图类渲染出来
  4. //将类中对其他类的实例化分离出来,降低耦合度
  5. //加载要用到的模型类和视图类
  6. require 'Model.php';
  7. require 'View.php';
  8. class Controller2{
  9. public function index(Model $model,View $view){
  10. //获取模型类的数据
  11. $data = $model->getData();
  12. //渲染给视图类
  13. return $view->fetch($data);
  14. }
  15. }
  16. $controller1 = new Controller2();
  17. $model = new Model();
  18. $view = new View();
  19. //传值给index方法
  20. echo $controller1->index($model,$view);
构造方法传参控制类
  1. <?php
  2. namespace mvc;
  3. //控制类:向模型类请求读取数据,再发送给视图类渲染出来
  4. //将类中对其他类的实例化分离出来,放在构造方法中,降低耦合度
  5. //加载要用到的模型类和视图类
  6. require 'Model.php';
  7. require 'View.php';
  8. class Controller3{
  9. protected $model;
  10. protected $view;
  11. public function __construct(Model $model,View $view){
  12. $this->model = $model;
  13. $this->view = $view;
  14. }
  15. public function index(){
  16. //获取模型类的数据
  17. $data = $this->model->getData();
  18. //渲染给视图类
  19. return $this->view->fetch($data);
  20. }
  21. }
  22. $model = new Model();
  23. $view = new View();
  24. //传值给Controller类的构造方法
  25. $controller1 = new Controller3($model,$view);
  26. echo $controller1->index();
服务容器层传参控制类
  1. <?php
  2. namespace mvc;
  3. //控制类:向模型类请求读取数据,再发送给视图类渲染出来
  4. //增加一个服务容器层,通过服务容器层来传递参数
  5. //加载要用到的模型类和视图类
  6. require 'Model.php';
  7. require 'View.php';
  8. //添加服务容器层
  9. class Container{
  10. //创建一个数组放创建对象的方法
  11. protected $instance = [];
  12. //将类的实例化绑定到容器
  13. public function bind($alias,\Closure $process){
  14. $this->instance[$alias] = $process;
  15. }
  16. //执行容器中的实例方法
  17. public function make($alias,$params=[]){
  18. return call_user_func_array($this->instance[$alias],[]);
  19. }
  20. }
  21. //实例化容器
  22. $container = new Container();
  23. //绑定对象到容器中
  24. $container->bind('model',function (){return new Model();});
  25. $container->bind('view',function (){return new View();});
  26. class Controller4{
  27. public function index(Container $container){
  28. //获取模型类的数据
  29. $data = $container->make('model')->getData();
  30. //渲染给视图类
  31. return $container->make('view')->fetch($data);
  32. }
  33. }
  34. //传值给Controller类的构造方法
  35. $controller1 = new Controller4();
  36. echo $controller1->index($container);
Facade门面类传参控制类
  1. <?php
  2. namespace mvc;
  3. //控制类:向模型类请求读取数据,再发送给视图类渲染出来
  4. //增加一个服务容器层,通过服务容器层来传递参数
  5. //增加Facade技术:规范对外部对象的调用方式,全部改为静态调用
  6. //加载要用到的模型类和视图类
  7. require 'Model.php';
  8. require 'View.php';
  9. //添加服务容器层
  10. class Container1{
  11. //创建一个数组放创建对象的方法
  12. protected $instance = [];
  13. //将类的实例化绑定到容器
  14. public function bind($alias,\Closure $process){
  15. $this->instance[$alias] = $process;
  16. }
  17. //执行容器中的实例方法
  18. public function make($alias,$params=[]){
  19. return call_user_func_array($this->instance[$alias],[]);
  20. }
  21. }
  22. //实例化容器
  23. $container = new Container1();
  24. //绑定对象到容器中
  25. $container->bind('model',function (){return new Model();});
  26. $container->bind('view',function (){return new View();});
  27. //添加Facade门面类
  28. class Facade{
  29. protected static $container = null;
  30. protected static $data = [];
  31. public static function initlize(Container1 $container){
  32. static::$container = $container;
  33. }
  34. public static function getData(){
  35. static::$data = static::$container->make('model')->getData();
  36. }
  37. public static function fetch(){
  38. return static::$container->make('view')->fetch(static::$data);
  39. }
  40. }
  41. class Controller5{
  42. public function __construct(Container1 $container){
  43. Facade::initlize($container);
  44. }
  45. public function index(){
  46. //获取模型类的数据
  47. Facade::getData();
  48. //渲染给视图类
  49. return Facade::fetch();
  50. }
  51. }
  52. //传值给Controller类的构造方法
  53. $controller = new Controller5($container);
  54. echo $controller->index();

手抄作业

总结

①、静态成员分为属性和方法,通过”static::”直接访问。
②、后期静态绑定可以替代“self”关键字,在运行时才绑定。
③、MVC框架分为三大类,Model类(模型),View类(视图),Controller类(控制),控制类通过模型类获取数据,再把数据分发给视图渲染。
④、控制类demo1-demo3的都可以理解,demo4的容器类和demo5门面类理解怎么写,可是意义不是很明确,具体的用处体现在哪里
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