Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:mvc流程都明白, 后面就好办了
demo1.php
类静态static
<?php
//静态static
namespace _1204;
use PDO;
//声明定义
class Demo1
{
//静态成员
public static $pdo;
public static $dsn = 'mysql:host=localhost;dbname=jason';
public static $username = 'root';
public 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 `account`',PDO::FETCH_ASSOC);
}
}
//实例化db1
$result = Demo1::select();
foreach ($result as $row) {
echo '<pre>' . print_r($row, true) . '</pre>';
}
demo2.php
后期绑定static
<?php
//后期静态绑定,延迟绑定
namespace _1204;
use PDO;
//声明db1类
class Db1{
//创建静态成员
protected static $pdo;
protected static $dsn = 'mysql:host=localhost;dbname=jason';
protected static $username = 'root';
protected static $password = '123456';
//创建静态方法
protected static function connect(){
//static表示谁调用他就是谁
static::$pdo = new PDO(static::$dsn,static::$username,static::$password);
}
//创建静态查询方法
public static function select(){
static::connect();
return static::$pdo->query('select * from `account`',PDO::FETCH_ASSOC);
}
}
//声明定义db1_1类,继承db1类
class Db1_1 extends Db1{
//子类修改静态成员
protected static $username = 'root';
protected static $password = 'root';
//重写父类方法
protected static function connect(){
static::$pdo = new PDO(static::$dsn,static::$username,static::$password);
}
}
//实例化db1_1
$result = Db1_1::select();
foreach ($result as $row){
echo '<pre>' . print_r($row, true) . '</pre>';
}
运行效果
model.php
模型
<?php
namespace mvc;
class Model{
public function getData(){
return [
['id'=>1,'name'=>'苹果电脑','model' => 'MacBook Pro','price'=>25800],
['id'=>2,'name'=>'华为手机','model' => '华为P30 Pro','price'=>5800],
['id'=>3,'name'=>'小爱同学','model' => '小爱音响','price'=>800],
];
}
}
view.php
视图
<?php
namespace mvc;
//视图层
class View
{
public function fetch($data)
{
$table = '<table>';
$table .= '<caption>商品信息表</caption>';
$table .= '<tr><th>ID</th><th>品名</th><th>型号</th><th>价格</th></tr>';
foreach ($data as $product) {
$table .= '<tr>';
$table .= '<td>' . $product['id'] . '</td>';
$table .= '<td>' . $product['name'] . '</td>';
$table .= '<td>' . $product['model'] . '</td>';
$table .= '<td>' . $product['price'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
echo '<style>
table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
caption {font-size: 1.2rem; margin-bottom: 10px;}
tr:first-of-type { background-color:lightblue;}
td,th {border: 1px solid}
td:first-of-type {text-align: center}
</style>';
controller.php
控制器
<?php
namespace mvc;
//引入视图和模型
require '../Models/model.php';
require '../Views/view.php';
//定义控制器类
class Controller1{
public function index(){
//创建模型类
$model = new Model();
//获取模型中的二维数组
$data = $model->getData();
//创建视图类
$view = new View();
//调用视图类并将模型给视图
return $view->fetch($data);
}
}
//实例化对象
$c1 = new Controller1();
echo $c1->index();
demo2.php
通过注入方式降低耦合
<?php
namespace mvc;
//引入视图模型类
require '../Views/view.php';
require '../Models/model.php';
class Controller2{
//外部引入降低耦合
public function index(Model $model,View $view){
//调用视图给数据
return $view->fetch($model->getData());
}
}
//实例化控制器
$c2 = new Controller2();
//实例化模型
$model = new Model();
//实例化视图
$view = new View();
echo $c2->index($model,$view);
demo3.php
通过构造方法注入方式降低耦合
<?php
//构造函数依赖绑定
namespace mvc;
//引入模型
require '../Models/model.php';
//引入视图
require '../Views/view.php';
//声明视图
class Controller3{
//定义模型视图成员
protected $model;
protected $view;
//构造函数
public function __construct(Model $model,View $view)
{
$this->view = $view;
$this->model = $model;
}
public function index(){
return $this->view->fetch($this->model->getData());
}
}
//实例化视图模型
$model = new Model();
$view = new View();
//实例化控制器
$c3 = new Controller3($model,$view);
echo $c3->index();
demo4.php
通过容器类方式降低耦合
<?php
namespace mvc;
require '../Models/model.php';
require '../Views/view.php';
//创建容器类
class Container{
//定义容器对象
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],[]);
}
}
//创建容器类
$container = new Container();
//加入执行函数绑定
$container->bind('model',function (){return new Model();});
$container->bind('view',function (){return new View();});
//创建控制器类
class Controller4{
public function index(Container $container){
//取出模型
$data = $container->make('model')->getData();
//取出视图并且赋值模型
return $container->make('view')->fetch($data);
}
}
$c4 = new Controller4();
echo $c4->index($container);
demo5.php
通过容器类 Facade门面类方式降低耦合
<?php
namespace mvc;
require '../Views/view.php';
require '../Models/model.php';
//创建服务器容器类
class Container1{
protected $instance = [];
//放入容器
public function bind($alias,\Closure $closure){
$this->instance[$alias] = $closure;
}
//取出
public function make($alias,$params=[]){
return call_user_func_array($this->instance[$alias],$params);
}
}
//实例化容器类
$container1 = new Container1();
$container1->bind('model',function (){return new Model();});
$container1->bind('view',function (){return new View();});
//创建facade门面类
class Facade{
//创建容器成员
protected static $container ;
//data成员
protected static $data = [];
//初始化方法
public static function init(Container1 $container1){
static::$container = $container1;
}
//获取data方法
public static function getData(){
static::$data = static::$container->make('model')->getData();
}
//显示view
public static function fetch(){
return static::$container->make('view')->fetch(static::$data);
}
}
//控制器类
class Controller5{
//构造函数
public function __construct(Container1 $container1)
{
Facade::init($container1);
}
public function index(){
//获取数据
Facade::getData();
//显示数据
return Facade::fetch();
}
}
//实例化控制器类
$c5 = new Controller5($container1);
echo $c5->index();
2.1运行效果
2.2将最后一个demo5.php中的代码, 手写提交
今天学习了类的静态成员方法和延迟绑定的静态,静态成员和方法不需要通过类的实例化来调用,直接(类名::方法名或者成员)即可调用,后期绑定用(static::)是代码更加通用,这样如果是子类重写父类方法时候,就会优先调用本类的;
然后学习了MVC,M:model模型,负责数据方面的事情,V:view视图:负责显示的事情,C:controll控制器,负责协调模型和视图的控制器,注意,视图和模型不能直接交互,只能通过控制器协调,这就是为了降低视图模型和控制器之间的耦合度,然后学习了通过容器和门面类来降低耦合度,第一遍直播看的很晕,在看了一遍回放后,大致明白这样写的好处,主要是为了降低耦合,让程序更加的通用健壮。