批改状态:合格
老师批语:
1.服务容器与Facade都是简化客户端(也就最终使用者)的操作
2.服务容器:是在调用前,提前把类实列保存到一个关联数组中,使用直接调用即可;通常通过一个类来实现,把需要使用的通过类函数绑定到类属性数组中,调用时,通过类函数直接调用即可
3.Facade技术主要用来简化类的调用;通过把一般类的方法和函数静态化,简化调用步骤(省去类实列化步骤);
4.Facade(门面技术)用到的初始化方法:initialize(){}
5.Facade(门面技术):主要是通过门面类把所有的类中的方法套壳静态化方便后面使用者调用;
6.控制类需要调用的类->把类实例化后绑定到服务容器中->门面技术(通过子类继承父类的属性,把服务容器类绑定门面类属性中中,然后子类把所需要的方法静态化)->控制类(就可以通过静态方法调用相关类方法)
5.代码案例
<?php//容器// use Closure;//服务容器class Container{protected $container=[];public function bind($name,Closure $class){$this->container[$name]=$class;}public function make($name,$params=[]){return call_user_func_array($this->container[$name],$params);}}//门面技术class Facade{static protected $container=null;static public function initialize(Container $container){static::$container=$container;}}class UserModel extends Facade{static public function select($n,$m){return static::$container->make("Model")->select($n,$m);}}class UserView extends Facade{static public function index($data){return static::$container->make("View")->index($data);}}//控制器class Controls{public function __construct(Container $container){Facade::initialize($container);}public function show($n,$m){$data=UserModel::select($n,$m);return UserView::index($data);}}include "Model.php";include "View.php";$container=new Container();$container->bind("Model",function(){return new Model();});$container->bind("View",function(){return new View();});// (new Controls())->show($container);(new Controls($container))->show(0,15);
运行结果:
1.composer:php语言的包依赖管理工具
2.composer的安装请参考:https://pkg.phpcomposer.com/#how-to-install-composer
3.composer切换镜像:
composer config repo.packagist composer https://packagist.phpcomposer.comcomposer config repo.packagist composer https://mirrors.aliyun.com/composer/4.packagist(包依赖网站):https://packagist.org/
5.composer常用命令:
6.composer常见组成文件
7.自动加载配置:autoload;在json配置文件中”autoload”字段中的配置方式
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号