一、 将课堂源代码, 全部写一遍, 在编辑器中写就可, 必须全部运行正确, 并加上自己理解的注释,不用手写
demo1
实例
<?php namespace _1204; use PDO; class Db1 { protected static $pdo; protected static $dsn = 'mysql:host=localhost;dbname=movie'; protected static $username = 'root'; protected static $password = 'root'; public static function connect() { self::$pdo = new PDO(self::$dsn,self::$username,self::$password); } public static function select() { self::connect(); return self::$pdo->query('select * from `users`',PDO::FETCH_ASSOC); } } $result = Db1::select(); foreach ($result as $v){ echo '<pre>' . print_r($result,true) . '</pre>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
demo2
实例
<?php namespace _1204; use PDO; class Db2 { protected static $pdo; protected static $dsn = 'mysql:host=localhost;dbname=movie'; protected static $username = 'root'; protected static $password = 'root1'; public static function connect() { self::$pdo = new PDO(self::$dsn,self::$username,self::$password); } public static function select() { static::connect(); return self::$pdo->query('select * from `users`',PDO::FETCH_ASSOC); } } class sub extends Db2 { protected static $username = 'root'; protected static $password = 'root'; public static function connect() { self::$pdo = new PDO(self::$dsn, self::$username, self::$password); } } $result = db2::select(); foreach ($result as $v){ echo '<pre>' . print_r($v,true) . '</pre>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
MVC demo1
实例
<?php namespace mvc; require 'Model1.php'; require 'View1.php'; class Controller1 { public function index() { //实例化Model1,调用里面的getData方法获取数据 $model = new Model1(); $data = $model->getData(); //实例化View1,调用里面的fetch方法,传递$data参数进行渲染 $view = new View1(); return $view->fetch($data); } } //访问 $controller = new Controller1(); echo $controller->index();
运行实例 »
点击 "运行实例" 按钮查看在线实例
MVC demo2 依赖注入
实例
<?php namespace mvc; require 'Model1.php'; require 'View1.php'; class Controller2 { public function index(Model1 $model,View1 $view) { //通过在类外面实例化模型和视图,再通过传参的方式注入到类中 $data = $model->getData(); return $view->fetch($data); } } //依赖注入 $model = new Model1(); $view = new View1(); //访问 $controller = new Controller2(); echo $controller->index($model,$view);
运行实例 »
点击 "运行实例" 按钮查看在线实例
MVC demo3 构造方法依赖注入
实例
<?php namespace mvc; require 'Model1.php'; require 'View1.php'; class Controller3 { protected $model; protected $view; // 注入点在构造方法中 public function __construct(Model1 $model,View1 $view) { $this->model = $model; $this->view = $view; } public function index() { $data = $this->model->getData(); return $this->view->fetch($data); } } //依赖注入 $model = new Model1(); $view = new View1(); //访问 $controller = new Controller3($model,$view); echo $controller->index();
运行实例 »
点击 "运行实例" 按钮查看在线实例
MVC demo4 容器
实例
<?php namespace mvc; require 'Model1.php'; require 'View1.php'; class Container { protected $instance = []; public function bind($alias,$process) { //把类的实例化方法绑定到容器中 $this->instance[$alias] = $process; } public function make($alias,$params=[]) { //取出实例化方法 return call_user_func_array($this->instance[$alias],[]); } } //把model和view的实例化过程绑定到容器中 $container = new Container(); $container->bind('model',function (){return new Model1();}); $container->bind('view',function (){return new View1();}); class Controller4 { protected $model; protected $view; public function index(Container $container) { $data = $container->make('model')->getData(); return $container->make('view')->fetch($data); } } //访问 $controller = new Controller4(); echo $controller->index($container);
运行实例 »
点击 "运行实例" 按钮查看在线实例
MVC demo5 门面模式
实例
<?php namespace mvc; require 'Model1.php'; require 'View1.php'; class Container1 { protected $instance = []; public function bind($alias,\Closure $process) { //把类的实例化方法绑定到容器中 $this->instance[$alias] = $process; } public function make($alias,$params=[]) { //取出实例化方法 return call_user_func_array($this->instance[$alias],[]); } } //把model和view的实例化过程绑定到容器中 $container = new Container1(); $container->bind('model',function (){return new Model1();}); $container->bind('view',function (){return new View1();}); class Facade { protected static $container = null; protected static $data = []; public static function initialize(Container1 $container) { static::$container = $container; } //静态化getData public static function getData() { static::$data = static::$container->make('model')->getData(); } //静态化fetch public static function fetch() { return static::$container->make('view')->fetch(static::$data); } } class Controller5 { public function __construct(Container1 $container) { Facade::initialize($container); } public function index() { Facade::getData(); return Facade::fetch(); } } //访问 $controller = new Controller5($container); echo $controller->index();
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、 将最后一个demo5.php中的代码, 手写提交
注意, 每一天的作业, 最后都应该有一个当天作业总结 , 这对老师掌握大家的学习情况非常重要, 谢谢配合
三、总结
1、静态类中成员访问类内用self::,类外用类名。静态方法中不允许用$this访问动态成员。
2、MVC先创建模型获取数据,再创建视图用于渲染,最后写控制器调用
3、依赖注入可以选择构造方法注入
4、服务容器就是把类名和其实例化的方法放进去并绑定,用的时候再调用出来
5、门面模式,就是让控制器使用静态方法调用其他类中成员