<?php
//命名空間
namespace mvctest;
//定義一個(gè)模型類:用來獲取數(shù)據(jù)
class Model {
public function getData() {
return (new \PDO('mysql:host=localhost;dbname=genbackmanasys', 'root', 'root'))->query('SELECT * FROM `user_info`')->fetchAll(\PDO::FETCH_ASSOC);
}
}
//調(diào)試代碼
//print_r((new Model)->getData());
<?php
//命名空間
namespace mvctest;
//定義一個(gè)視圖類:用來渲染數(shù)據(jù)
class View {
public function fetch($data) {
//使用字符串拼接的方式渲染數(shù)據(jù)
$table = '<table>';
$table .= '<caption>用戶信息表</caption>';
$table .= '<tr>
<th>ID</th>
<th>用戶名</th>
<th>性別</th>
<th>職位</th>
<th>郵箱</th>
<th>手機(jī)號(hào)碼</th>
<th>城市</th>
<th>個(gè)性簽名</th>
<th>注冊時(shí)間</th>
</tr>';
//將數(shù)據(jù)循環(huán)遍歷出來
foreach ($data as $staff) {
$table.='<tr>';
$table.='<td>' . $staff['id'] . '</td>';
$table.='<td>' . $staff['user_name'] . '</td>';
$table.='<td>' . $staff['sex'] . '</td>';
$table.='<td>' . $staff['position'] . '</td>';
$table.='<td>' . $staff['email'] . '</td>';
$table.='<td>' . $staff['cphone_n'] . '</td>';
$table.='<td>' . $staff['city'] . '</td>';
$table.='<td>' . $staff['signature'] . '</td>';
$table.='<td>' . date('Y年m月d日', $staff['reg_time']) . '</td>';
$table.='<tr>';
}
$table .= '</table>';
return $table;
}
}
//定義樣式
echo '<style>
table {border-collapse: collapse;border: 1px solid;}
th, td{border: 1px solid; padding: 5px;}
</style>';
//調(diào)試代碼
//require 'Model.php';
//echo (new View)->fetch((new Model)->getData());
<?php
//命名空間
namespace mvctest;
// 1. 加載模型類
require 'Model.php';
// 2. 加載視圖類
require 'View.php';
// 3. 創(chuàng)建服務(wù)容器類:統(tǒng)一管理類實(shí)例
class Container {
// 1. 創(chuàng)建對象容器
protected $box = [];
// 2. 創(chuàng)建綁定方法:向?qū)ο笕萜髦刑砑右粋€(gè)類實(shí)例
public function bind($var, \Closure $process) {
//對象容器中的鍵是對象名,值是其實(shí)例化過程
$this->box[$var] = $process;
}
// 3. 創(chuàng)建取出方法:從容器中取出一個(gè)類實(shí)例(new的過程)
public function make($var, $params = []) {
//用回調(diào)方式返回一個(gè)對象
return call_user_func_array($this->box[$var], []);
}
}
// 4. 創(chuàng)建門面類:接管對容器對象的訪問
class Facade {
//創(chuàng)建一個(gè)容器,用來保存外部對象
protected static $container = null;
//創(chuàng)建一個(gè)容器,用來保存實(shí)例屬性
protected static $data = [];
//初始化方法:從外部接受一個(gè)依賴對象,該對象為服務(wù)容器的實(shí)例
public static function initialize(Container $container)
{
//初始化容器實(shí)例屬性
static::$container = $container;
}
}
//模型成員靜態(tài)化
class Model1 extends Facade {
//以靜態(tài)方法訪問的方式獲取數(shù)據(jù)
public static function getData() {
static::$data = static::$container->make('model')->getData();
}
}
//視圖成員靜態(tài)化
class View1 extends Facade {
//以靜態(tài)方法訪問的方式返回視圖
public static function fetch() {
return static::$data = static::$container->make('view')->fetch(static::$data);
}
}
// 5. 創(chuàng)建控制器類:將用戶請求和數(shù)據(jù)進(jìn)行關(guān)聯(lián)/綁定
class Controller {
//構(gòu)造方法:調(diào)用門面的初始化方法
public function __construct(Container $container)
{
Facade::initialize($container);
}
public function bind() {
// 1. 獲取數(shù)據(jù)
Model1::getData();
// 2. 渲染模板/視圖
return View1::fetch();
}
}
//客戶端代碼
// 1. 創(chuàng)建服務(wù)容器
$container = new Container;
// 2. 綁定
$container->bind('model', function() {return new Model;});
$container->bind('view', function() {return new View;});
// 3. 實(shí)例化控制器類
$controller = new Controller($container);
echo $controller->bind();
微信掃碼
關(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)