Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:今天会讲到具体的应用场景
<?php
namespace _1204;
header('content-type:text/html;charset=utf-8');
use PDO;
//static:声明类中的静态成员
class Db1{
//静态成员包含:属性,成员
protected static $pdo;
protected static $dsn = 'mysql:host=localhost;dbname=demo';
protected static $user = 'root';
protected static $pwd = 'root';
public static function connect(){
//在类中访问当前类的静态成员:self
self::$pdo = new PDO(self::$dsn,self::$user,self::$pwd);
}
//测试方法
public static function select(){
//连接数据库
self::connect();
return self::$pdo->query('select * from `user`',PDO::FETCH_ASSOC);
}
}
//在类外部,直接用类名访问静态成员
$result = Db1::select();
//print_r($result);
foreach ($result as $value) {
echo '<pre>'.print_r($value,true);
}
<?php
namespace _1204;
header('content-type:text/html;charset=utf-8');
use PDO;
//static:后期静态绑定
//使用场景:静态继承的上下文环境中
//后期绑定就是在调用类成员时绑定,也叫延迟绑定
//可以拿来替代self关键字
class Db2{
//静态成员包含:属性,成员
protected static $pdo;
protected static $dsn = 'mysql:host=localhost;dbname=demo';
protected static $user = 'root';
protected static $pwd = 'root';
public static function connect(){
//在类中访问当前类的静态成员:self
static::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
}
//测试方法
public static function select(){
//连接数据库
static::connect();
return self::$pdo->query('select * from `user`',PDO::FETCH_ASSOC);
}
}
//创建子类,重新定义connet方法,根据user和pwd的不同值
class Sub extends Db2{
protected static $user = 'zhangsan';
protected static $pwd = '123456';
public static function connect(){
self::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
}
}
//在类外部,直接用类名访问静态成员
//Db2的user和pwd是正确的,所以可以读取数据库数据
$result = Db2::select();
//print_r($result);
foreach ($result as $value) {
echo '<pre>'.print_r($value,true);
}
//sub类重新定义错误的user和pwd值,不能读取数据
$result1 = Sub::select();
print_r($result1);
<?php
namespace mvc;
header('content-type:text/html;charset=utf-8');
//模型类:用于数据表的操作
use PDO;
class Model{
private static $pdo;
private static $dsn = 'mysql:host=localhost;dbname=demo';
private static $user = 'root';
private static $pwd = 'root';
private static $sql = 'select * from `product`';
public static function connect(){
static::$pdo = new PDO(static::$dsn,static::$user,static::$pwd);
}
public function getData(){
static::connect();
return static::$pdo->query(static::$sql,PDO::FETCH_ASSOC);
}
}
<?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 $value) {
$table .= '<tr>';
$table .= '<td>' . $value['id'] . '</td>';
$table .= '<td>' . $value['name'] . '</td>';
$table .= '<td>' . $value['model'] . '</td>';
$table .= '<td>' . $value['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>';
<?php
namespace mvc;
//控制类:向模型类请求读取数据,再发送给视图类渲染出来
//加载要用到的模型类和视图类
require 'Model.php';
require 'View.php';
class Controller1{
public function index(){
//获取模型类的数据
$model = new Model();
$data = $model->getData();
//渲染给视图类
$view = new View();
return $view->fetch($data);
}
}
$controller1 = new Controller1();
echo $controller1->index();
<?php
namespace mvc;
//控制类:向模型类请求读取数据,再发送给视图类渲染出来
//将类中对其他类的实例化分离出来,降低耦合度
//加载要用到的模型类和视图类
require 'Model.php';
require 'View.php';
class Controller2{
public function index(Model $model,View $view){
//获取模型类的数据
$data = $model->getData();
//渲染给视图类
return $view->fetch($data);
}
}
$controller1 = new Controller2();
$model = new Model();
$view = new View();
//传值给index方法
echo $controller1->index($model,$view);
<?php
namespace mvc;
//控制类:向模型类请求读取数据,再发送给视图类渲染出来
//将类中对其他类的实例化分离出来,放在构造方法中,降低耦合度
//加载要用到的模型类和视图类
require 'Model.php';
require 'View.php';
class Controller3{
protected $model;
protected $view;
public function __construct(Model $model,View $view){
$this->model = $model;
$this->view = $view;
}
public function index(){
//获取模型类的数据
$data = $this->model->getData();
//渲染给视图类
return $this->view->fetch($data);
}
}
$model = new Model();
$view = new View();
//传值给Controller类的构造方法
$controller1 = new Controller3($model,$view);
echo $controller1->index();
<?php
namespace mvc;
//控制类:向模型类请求读取数据,再发送给视图类渲染出来
//增加一个服务容器层,通过服务容器层来传递参数
//加载要用到的模型类和视图类
require 'Model.php';
require '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);
}
}
//传值给Controller类的构造方法
$controller1 = new Controller4();
echo $controller1->index($container);
<?php
namespace mvc;
//控制类:向模型类请求读取数据,再发送给视图类渲染出来
//增加一个服务容器层,通过服务容器层来传递参数
//增加Facade技术:规范对外部对象的调用方式,全部改为静态调用
//加载要用到的模型类和视图类
require 'Model.php';
require 'View.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],[]);
}
}
//实例化容器
$container = new Container1();
//绑定对象到容器中
$container->bind('model',function (){return new Model();});
$container->bind('view',function (){return new View();});
//添加Facade门面类
class Facade{
protected static $container = null;
protected static $data = [];
public static function initlize(Container1 $container){
static::$container = $container;
}
public static function getData(){
static::$data = static::$container->make('model')->getData();
}
public static function fetch(){
return static::$container->make('view')->fetch(static::$data);
}
}
class Controller5{
public function __construct(Container1 $container){
Facade::initlize($container);
}
public function index(){
//获取模型类的数据
Facade::getData();
//渲染给视图类
return Facade::fetch();
}
}
//传值给Controller类的构造方法
$controller = new Controller5($container);
echo $controller->index();