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:451390browse

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

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

The following Integrate the code together:

<?php 
//数据库操作类
class Db
{
//数据库连接
public function connect()
{
return &#39;数据库连接成功<br>&#39;;
}
}
//数据验证类
class Validate
{
//数据验证
public function check()
{
return &#39;数据验证成功<br>&#39;;
}
}
//视图图
class View
{
//内容输出
public function display()
{
return &#39;用户登录成功&#39;;
}
}
/******************************************************************************/
//一.创建容器类
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(&#39;db&#39;, function(){
return new Db();
});
//将Validate类实例绑定到容器中
$container->bind(&#39;validate&#39;, function(){
return new Validate();
});
//将View类实例绑定到容器中
$container->bind(&#39;view&#39;, 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 (www.php.cn).

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