Home>Article>Backend Development> Let’s talk about dependency injection, containers and appearance patterns in framework development (Part 2)

Let’s talk about dependency injection, containers and appearance patterns in framework development (Part 2)

不言
不言 Original
2018-07-14 13:47:45 1313browse

This article mainly introduces the dependency injection, container and appearance mode (lower part) about chatting framework development. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Appearance mode: facade, also called facade mode

1. In one sentence: it encapsulates operations and provides a unified interface to the outside world

2. Because operations may be distributed in multiple classes , and the container I just learned can encapsulate different classes and implementations

3. Therefore, appearance mode and dependent containers are golden partners, and are often used together

make('db')->connect(); } //用户数据验证 public static function check(Container $container) { return $container->make('validate')->check(); } //输出提示信息 public static function display(Container $container) { return $container->make('view')->display(); } } //客户端调用 echo Facade::connect($container); echo Facade::check($container); echo Facade::display($container); //可以在外观模型中使用初始化方法事先注入容器对象,来简化客户端调用 require 'container.php'; class Facade { //创建成员属性保存容器对象 protected static $container = null; //创建初始化方法为容器对象赋值 public static function initialize(Container $container) { static::$container = $container; } /** * 因为已经在初始化方法中将容器对象导入到了当前类中, * 并且保存到了类的静态属性中,为所有类成员所共享, * 所以以下方法可直接调用不用再容器注入 * 注意:这里全部采用后期静态延迟绑定方法来访问当前容器对象 * 这主要是为了方便用户在静态继承的上下文环境中进行调用 */ //连接数据库 public static function connect() { return static::$container->make('db')->connect(); } //用户数据验证 public static function check() { return static::$container->make('validate')->check(); } //输出提示信息 public static function display() { return static::$container->make('view')->display(); } } //客户端调用 //初始化类门面类中的容器对象 Facade::initialize($container); //静态统一调用内部的方法(无须重复注入依赖容器对象啦,实现了细节隐藏,通用性更强) echo Facade::connect(); echo Facade::check(); echo Facade::display();

The following Integrate the code together:

'; } } //数据验证类 class Validate { //数据验证 public function check() { return '数据验证成功
'; } } //视图图 class View { //内容输出 public function display() { return '用户登录成功'; } } /******************************************************************************/ //一.创建容器类 class Container { //创建属性,用空数组初始化,该属性用来保存类与类的实例化方法 public $instance = []; //初始化实例数组,将需要实例化的类,与实例化的方法进行绑定 public function bind($abstract, Closure $process) { //键名为类名,值为实例化的方法 $this->instance[$abstract] = $process; } //创建类实例 public function make($abstract, $params=[]) { return call_user_func_array($this->instance[$abstract],[]); } } /******************************************************************************/ //二、服务绑定: 将类实例注册到容器中 $container = new Container(); //将Db类绑定到容器中 $container->bind('db', function(){ return new Db(); }); //将Validate类实例绑定到容器中 $container->bind('validate', function(){ return new Validate(); }); //将View类实例绑定到容器中 $container->bind('view', function(){ return new View(); });

Starting from php5.3, php has absorbed a large number of advantages of other programming languages and supported more and more new features, especially the launch of php7.0, which has made php reach the A new height is a landmark version. For more excellent PHP development tutorials, please continue to pay attention to: PHP Chinese website (m.sbmmt.com).

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Let’s talk about dependency injection, containers and appearance patterns in framework development (Part 1)

Let’s talk Dependency injection, container and appearance pattern for framework development (middle part)

The above is the detailed content of Let’s talk about dependency injection, containers and appearance patterns in framework development (Part 2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn