
批改狀態(tài):合格
老師批語(yǔ):其實(shí)這些都是一些最簡(jiǎn)單的業(yè)務(wù)思想, 一定要領(lǐng)會(huì)
C :Controller,控制器
View->Controller->Model 前端視圖->處理操作->后端操作
示例:
<?php
namespace MVC_test;
use PDO;
class Model
{
public function myDate()
{
return ($pdo = new PDO('mysql:host=localhost;dbname=mysqli','root','142536'))
->query('select * from user limit 10')->fetchAll(PDO::FETCH_ASSOC);
}
}
示例圖:
Model.php 功能主要是一個(gè)連接數(shù)據(jù)庫(kù)類
View.php 前端視圖
<?php
namespace MVC_test;
class View
{
public function mysHow($data)
{
$table = '<table>';
$table.='<tr></tr>';
$table .= '<caption>用戶信息表</caption>';
$table .= '<tr><th>用戶名</th><th>性別</th><th>年齡</th><th>郵箱</th><th>手機(jī)</th></tr>';
foreach($data as $user)
{
$table .= '<tr>';
$table .= '<td>'.$user['username'].'</td>';
$table .= '<td>'.$user['sex'].'</td>';
$table .= '<td>'.$user['age'].'</td>';
$table .= '<td>'.$user['email'].'</td>';
$table .= '<td>'.$user['mobile'].'</td>';
}
$table.='<tr></table>';
return $table;
}
}
echo '<style>
table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
caption {font-size: 1.2rem; margin-bottom: 10px;}
tr:first-of-type { background-color:yellow;}
td,th {border: 1px solid; padding:5px}
</style>';
示例圖:
示例:
<?php
namespace MVC_test;
require 'Model.php';
require 'View.php';
class Controller
{
public function Index()
{
$data = (new Model)->myDate();
//引入Model.php的myDate方法(鏈接數(shù)據(jù)庫(kù))
return (new View)->mysHow($data);
//返回引入View.php的mysHow方法(展示html)
}
}
示例
<?php
namespace MVC_test;
require 'Model.php';
require 'View.php';
class Controller_1
{
private $data = NULL;
private $show = NULL;
public function __construct($data,$show)
{
$this->data = $data;
$this->show = $show;
}
public function Index()
{
$this->data = (new Model)->myDate();
return $this->show = (new View)->mysHow($this->data);
}
}
示例:
<?php
namespace MVC_test;
use Closure;
//加載模型類
require 'Model.php';
//加載視圖類
require 'View.php';
//服務(wù)容器
class Container
{
// 1. 服務(wù)容器
protected $instances = [];
//定義一個(gè)空數(shù)組來(lái)存儲(chǔ)對(duì)象
public function bInd($alias, Closure $process)
// 2. 向?qū)ο笕萜髦刑砑訉?duì)象
// 參數(shù)1: 是外部對(duì)象在當(dāng)前對(duì)象容器數(shù)組中的鍵名/別名
// 參數(shù)2: 是當(dāng)前需要綁定到容器的對(duì)象的實(shí)例化過(guò)程(函數(shù))
{
$this->instances[$alias] = $process;
//將實(shí)例化過(guò)程寫入服務(wù)容器
}
public function make($alias,$params=[])
// 3. 從對(duì)象容器中取出對(duì)象, 調(diào)用它
// 參數(shù)1:傳入服務(wù)容器中的別名
// 參數(shù)2:一個(gè)空的數(shù)組
{
return call_user_func_array($this->instances[$alias], []);
// 從instances中取出對(duì)象名
// call_user_func_array :調(diào)用回調(diào)函數(shù),并把一個(gè)數(shù)組參數(shù)作為回調(diào)函數(shù)的參數(shù)。
}
}
// 將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
$container = new Container;
// 綁定模型類實(shí)例綁定到服務(wù)容器中
$container->bInd('model', function(){
return new Model();
});
$container->bInd('view',function(){
return new View();
});
class Controller_2
{
public function Index(Container $container)
{
$data = $container->make('model')-> myDate();
return $container->make('view')->mysHow($data);
}
}
示例:
<?php
namespace MVC_test;
use Closure;
//加載模型類
require 'Model.php';
//加載視圖類
require 'View.php';
//服務(wù)容器
class Container_1
{
// 1. 服務(wù)容器
protected $instances = [];
//定義一個(gè)空數(shù)組來(lái)存儲(chǔ)對(duì)象
public function bInd($alias, Closure $process)
// 2. 向?qū)ο笕萜髦刑砑訉?duì)象
// 參數(shù)1: 是外部對(duì)象在當(dāng)前對(duì)象容器數(shù)組中的鍵名/別名
// 參數(shù)2: 是當(dāng)前需要綁定到容器的對(duì)象的實(shí)例化過(guò)程(函數(shù))
{
$this->instances[$alias] = $process;
//將實(shí)例化過(guò)程寫入服務(wù)容器
}
public function make($alias,$params=[])
// 3. 從對(duì)象容器中取出對(duì)象, 調(diào)用它
// 參數(shù)1:傳入服務(wù)容器中的別名
// 參數(shù)2:一個(gè)空的數(shù)組
{
return call_user_func_array($this->instances[$alias], []);
// 從instances中取出對(duì)象名
// call_user_func_array :調(diào)用回調(diào)函數(shù),并把一個(gè)數(shù)組參數(shù)作為回調(diào)函數(shù)的參數(shù)。
}
}
// 將外部對(duì)象: Model, View的實(shí)例綁定到服務(wù)容器中
$container = new Container_1;
// 綁定模型類實(shí)例綁定到服務(wù)容器中
$container->bInd('model', function(){
return new Model();
});
$container->bInd('view',function(){
return new View();
});
// --------------------------------------------------------
class Facade
{
//服務(wù)容器
protected static $container =NULL;
// 初始化方法:就是給當(dāng)前的Facade類扣$container屬性賦值
//整理為Facade的構(gòu)造方法
//將外部的服務(wù)容器注入到當(dāng)前的facade中
public static function initialize(Container_1 $container)
{
static::$container = $container;
}
}
class UserModel extends Facade
{
public static function myDate()
{
return static::$container->make('model')->myDate();
}
}
class UserView extends Facade
{
public static function mysHow($data)
{
return static::$container->make('view')->mysHow($data);
}
}
// --------------------------------------------------------
class Controller_3
{
//構(gòu)造方法,初始化facade
public function __construct(Container_1 $container)
{
Facade::initialize($container);
}
public function Index()
{
//1.獲取數(shù)據(jù)
$data =UserModel::myDate();
//2.渲染模板
return UserView::mysHow($data);
}
}
示例:
<?php
namespace MVC_test;
// --------------------------
// require 'Controller.php';
// $index = new Controller;
// echo $index->Index();
// ------------>Controller控制器的引用
// require 'Controller_1.php';
// $index = new Controller_1($data,$show);
// echo $index->Index();
// ------------>Controller_1控制器的引用
// --------服務(wù)容器----------
// require 'Controller_2.php';
// $index = new Controller_2();
// echo $index->Index($container);
// ------------>Controller_2控制器的引用
//-----------Facade-----------
require 'Controller_3.php';
$index = new Controller_3($container);
echo $index->Index();
總結(jié):在對(duì)MVC的學(xué)習(xí)中,掌握了服務(wù)容器及facade對(duì)類實(shí)例化對(duì)象的使用。不需要再對(duì)每個(gè)類實(shí)例化繁瑣調(diào)用。也大體的掌握了面對(duì)對(duì)象編程的業(yè)務(wù)邏輯。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)