批改状态:合格
老师批语:依赖注入, 是不是比想像中简单呢?
1、写一个依赖注入的案例
<?php
class Test1
{
protected $val;
public function set($val){
return $this->val = $val;
}
}
//1、基本方式实现
class Test2
{
public function get(){
$test1 = new Test1();
echo $test1->set('循序渐进实现容器依赖注入方式----基本方式实现');
}
}
$test2 = new Test2();
$test2->get();
echo '<hr>';
//2、构造方式 或者 方法传参方式
class Test3
{
protected $test1;
public function __construct(Test1 $test1){
$this->test1 = $test1;
}
public function get(){
echo $this->test1->set('循序渐进实现容器依赖注入方式----构造函数传对象方式实现');
}
}
$test1 = new Test1();
$test2 = new Test3($test1);
$test2->get();
echo '<hr>';
//3、采用容器方式进行管理对象
class Container
{
//用于存储对象数组
static protected $data = [];
//创建对象 采用闭包方式 通过回调函数返回对象
public function bind($name, $Func){
self::$data[$name] = $Func;
}
//获取对象,并采用回调函数 调用类中的方法
public function make($name,$args =[]){
return call_user_func(self::$data[$name],$args);
}
}
$container = new Container();
$container->bind('Test1',function(){
return new Test1();
});
class Test4
{
public function get(Container $container){
return $container->make('Test1')->set('循序渐进实现容器依赖注入方式----采用容器管理创建返回对象');
}
}
$test4 = new Test4();
print_r($test4->get($container));
echo '<hr>';
//4、采用门面方式实现静态方式调用
class Container1
{
//用于存储对象数组
static protected $data = [];
//创建对象 采用闭包方式 通过回调函数返回对象
public function bind($name, $Func){
self::$data[$name] = $Func;
}
//获取对象,并采用回调函数 调用类中的方法
public function make($name,$args =[]){
return call_user_func(self::$data[$name],$args);
}
}
$container = new Container1();
$container->bind('Test1',function(){
return new Test1();
});
//门面类
class Facade
{
//容器对象
protected static $container = null;
//存放数据数组
static protected $data = [];
//初始化容器对象
public static function init(Container1 $container){
static::$container = $container;
}
//数据存储
static public function setDate(){
self::$data = self::$container->make('Test1')->set('循序渐进实现容器依赖注入方式----采用容器管理创建返回对象');
}
//进行渲染的方法
static public function fetch(){
return self::$container->make('Test1')->set(self::$data);
}
}
//创建一个商品类
class Car extends Facade
{
}
class Test5
{
public function get(Container1 $container){
Car::init($container);
Car::setDate();
return Car::fetch();
}
}
$test4 = new Test5();
print_r($test4->get($container));点击 "运行实例" 按钮查看在线实例
2、写一个MVC应用的案例
//view.php
<?php
class View
{
function fetch($data){
echo $data;
}
}
//model.php
class Model
{
function getVal(){
return 'MVC模式获取数据';
}
}
//controller.php
include 'model.php';
include 'view.php';
class Controller
{
function index(){
$model = new Model();
$view = new View();
$data = $model->getVal();
return $view->fetch($data);
}
}
$obj = new Controller();
$obj->index();点击 "运行实例" 按钮查看在线实例
3、写一个简单的路由
<?php
//"http://demo.com/10-11/router.php/admin/Index/add/a/1/b/2/c/3";
$uri = "/10-11/router.php/admin/Index/add/a/1/b/2/c/3";//$_SERVER["REQUEST_URI"];
$uriArr = explode('/',$uri);
$class = $uriArr[4];
$action = $uriArr[5];
$param = array_slice($uriArr,6);
for($i=0;$i<count($param);$i+=2){
$params[$param[$i]] = $param[$i+1];
}
class Index
{
function add($a,$b,$c){
echo $a + $b + $c;
}
}
call_user_func_array([new $class(),$action],$params);点击 "运行实例" 按钮查看在线实例
总结:
1、单例模式:三私一公 三私:对象实例用于存储、__construct防止实例化、__clone防止克隆 一公:对外访问进行生成实例进行存储判断,防止多次创建
2、工厂模式:专门生成对象的工厂
3、依赖注入:将对象作为参数进行传递,可以实现低耦合,防止调用对象修改导致其他引用修改
4、MVC模式:model 模型进行crud处理数据 view 视图进行页面展示 controller 控制器进行引入模型和视图进行返回
5、路由:将url中的操作映射到模块对应控制器中的方法上并携带参数值,通过获取url中的控制器及方法来触发相应的对象方法
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号