
批改狀態(tài):合格
老師批語:你這不能算實(shí)現(xiàn), 應(yīng)該寫一個(gè)真實(shí), 且相對(duì)完整的, 將學(xué)過的知識(shí), 盡可能的用上
<?php
namespace __1206;
// 模型類:用于數(shù)據(jù)表的操作
class Model{
public function getData()
{
// 用二維數(shù)組來模擬從表中獲取到的客戶數(shù)據(jù)
return [
['id'=>1,'name'=>'不甜的淡月','sex'=>'女','mobile'=>'452557923','email'=>'452557923@qq.com','vip'=>'璀璨鉆石'],
['id'=>2,'name'=>'溪上花間','sex'=>'女','mobile'=>'437366515','email'=>'437366515@qq.com','vip'=>'華貴鉑金'],
['id'=>3,'name'=>'小小挽歌','sex'=>'男','mobile'=>'1047826346','email'=>'104782346@qq.com','vip'=>'榮耀黃金'],
['id'=>4,'name'=>'櫻桃小丸子','sex'=>'男','mobile'=>'2244423838','email'=>'2244423838@qq.com','vip'=>'不屈白銀'],
['id'=>5,'name'=>'夢(mèng)幻泡沫','sex'=>'男','mobile'=>'2502704500','email'=>'2502704500@qq.com','vip'=>'英勇黃銅'],
];
}
}
<?php
namespace __1206;
// 視圖類:渲染數(shù)據(jù)
class View{
public function fetch($data){
$table = '<table>';
$table .= '<caption>公交車群內(nèi)成員信息</caption>';
$table .= '<tr><th>ID</th><th>昵稱</th><th>性別</th><th>聯(lián)系方式</th><th>郵箱</th><th>段位等級(jí)</th></tr>';
foreach ($data as $news) {
$table .= '<tr>';
$table .= '<td>' . $news['id'] . '</td>';
$table .= '<td>' . $news['name'] . '</td>';
$table .= '<td>' . $news['sex'] . '</td>';
$table .= '<td>' . $news['mobile'] . '</td>';
$table .= '<td>' . $news['email'] . '</td>';
$table .= '<td>' . $news['vip'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
echo '<style>
table {border-collapse: collapse; border: 1px solid; width: 600px;height: 200px}
caption {font-size: 1.2rem; margin-bottom: 10px;}
tr:first-of-type { background-color:pink;}
td,th {border: 1px solid}
td:first-of-type {text-align: center;}
</style>';
<?php
// 控制器: 將客戶信息表展示出來
namespace __1206;
// 1. 加載模型
require 'Model.php';
// 2. 加載視圖
require 'View.php';
//添加服務(wù)容器層
class Container{
// 容器屬性, 就是一個(gè)數(shù)組,里面全是創(chuàng)建對(duì)象的方法
protected $instance = [];
// 1. 放進(jìn)去: 將類的實(shí)例化過程綁定到容器中 $alias: 類實(shí)例的別名,
public function bind($alias, \Closure $process){
// 將類實(shí)例化的方法綁定/ 存儲(chǔ)到服務(wù)容器中
$this->instance[$alias] = $process;
}
// 2. 取出來: 執(zhí)行容器中的實(shí)例方法
public function make($alias, $params=[]){
return call_user_func_array($this->instance[$alias], []);
}
}
// 實(shí)例化容器
$container = new Container();
// 用到模型對(duì)象, 視圖對(duì)象,將它們綁定到容器中
$container->bind('model', function () {return new Model();});
$container->bind('view', function () {return new View();});
// Facade技術(shù): 規(guī)范/統(tǒng)一了對(duì)外部對(duì)象的調(diào)用方式, 全部改為了靜態(tài)調(diào)用, 不管之前的方法是什么類型
// 添加Facade門面類
class Facade{
protected static $container = null;
protected static $data = [];
// 用服務(wù)容器給它初始化
public static function initialize(Container $container){
static::$container = $container;
}
// 用靜態(tài)代理方式將模型中的getData()靜態(tài)化
public static function getData(){
static::$data = static::$container->make('model')->getData();
}
// 用靜態(tài)代理方式將視圖中的fetch()靜態(tài)化
public static function fetch(){
return static::$container->make('view')->fetch(static::$data);
}
}
class Student extends Facade{
//...
}
// 3. 創(chuàng)建控制器
class Controller{
public function __construct(Container $container){
// 調(diào)用Facade里面的初始化方法
Student::initialize($container);
}
public function index(){
// 3.1 獲取數(shù)據(jù)
Student::getData();
// 3.2 渲染模板
return Student::fetch();
}
}
// 4. 客戶端調(diào)用/訪問類成員 將模型對(duì)象與視圖對(duì)象,以參數(shù)的方式再次注入到控制器的方法
$controller = new Controller($container);
echo $controller->index();
感覺mvc模式挺簡(jiǎn)單的,或許是我了解的還不夠深入吧??磥磉€是需要更進(jìn)一步的加強(qiáng)學(xué)習(xí)。
微信掃碼
關(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)