Home Backend Development PHP Tutorial 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)

Jul 14, 2018 pm 01:47 PM
dependency injection container

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();
Copy after login

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();
});
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

AtomHub, an open source container mirroring center jointly created by Huawei, Inspur and other units, announced that it is officially open for public testing and can stably download domestic services. AtomHub, an open source container mirroring center jointly created by Huawei, Inspur and other units, announced that it is officially open for public testing and can stably download domestic services. Jan 02, 2024 pm 03:54 PM

According to Huawei’s official news, the Open Atomic Developer Conference, with the theme of “Everything for Developers”, was held in Wuxi for two days, from December 16 to 17. The conference was led by the Open Atomic Open Source Foundation, Huawei, and Inspur. , DaoCloud, Xieyun, Qingyun, Hurricane Engine, as well as the OpenSDV Open Source Alliance, openEuler community, OpenCloudOS community and other member units jointly initiated the construction of the AtomHub Trusted Mirror Center, which is officially open for public testing. AtomHub adheres to the concepts of co-construction, co-governance, and sharing, and aims to provide open source organizations and developers with a neutral, open and co-constructed trusted open source container mirror center. In view of the instability and uncontrollability of image warehouses such as DockerHub, and some

How to use Docker for container failure recovery and automatic restart How to use Docker for container failure recovery and automatic restart Nov 07, 2023 pm 04:28 PM

As a lightweight virtualization platform based on container technology, Docker has been widely used in various scenarios. In a production environment, high availability and automatic failure recovery of containers are crucial. This article will introduce how to use Docker for container failure recovery and automatic restart, including specific code examples. 1. Configuration of automatic container restart In Docker, the automatic restart function of the container can be enabled by using the --restart option when running the container. Common options are: no: do not automatically restart. silent

How to install Redhat Podman on Windows 10 or 11 via CMD How to install Redhat Podman on Windows 10 or 11 via CMD Oct 02, 2023 pm 09:33 PM

Install RedHatPodman on Windows 11 or 10 Follow the steps below to install RedHatPodman on your Windows machine using Command Prompt or Powershell: Step 1: Check System Requirements First, you have to make sure that your Windows system is running with the latest updates so that it can meet the requirements to run Podman requirements. You should be using Windows 11 or Windows 10 version 1709 (Build 16299) or higher and you have to enable Windows Subsystem for Linux 2 (WSL2) and VM features, well if they are not activated yet then you can use The two-step command executes this

A step-by-step guide to understanding dependency injection in Angular A step-by-step guide to understanding dependency injection in Angular Dec 02, 2022 pm 09:14 PM

This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

How to use dependency injection (Dependency Injection) in the Phalcon framework How to use dependency injection (Dependency Injection) in the Phalcon framework Jul 30, 2023 pm 09:03 PM

Introduction to the method of using dependency injection (DependencyInjection) in the Phalcon framework: In modern software development, dependency injection (DependencyInjection) is a common design pattern aimed at improving the maintainability and testability of the code. As a fast and low-cost PHP framework, the Phalcon framework also supports the use of dependency injection to manage and organize application dependencies. This article will introduce you how to use the Phalcon framework

How to sort C++ STL containers? How to sort C++ STL containers? Jun 02, 2024 pm 08:22 PM

How to sort STL containers in C++: Use the sort() function to sort containers in place, such as std::vector. Using the ordered containers std::set and std::map, elements are automatically sorted on insertion. For a custom sort order, you can use a custom comparator class, such as sorting a vector of strings alphabetically.

What are the common types in C++ STL containers? What are the common types in C++ STL containers? Jun 02, 2024 pm 02:11 PM

The most common container types in C++STL are Vector, List, Deque, Set, Map, Stack and Queue. These containers provide solutions for different data storage needs, such as dynamic arrays, doubly linked lists, and key- and value-based associative containers. In practice, we can use STL containers to organize and access data efficiently, such as storing student grades.

Dependency injection pattern in Golang function parameter passing Dependency injection pattern in Golang function parameter passing Apr 14, 2024 am 10:15 AM

In Go, the dependency injection (DI) mode is implemented through function parameter passing, including value passing and pointer passing. In the DI pattern, dependencies are usually passed as pointers to improve decoupling, reduce lock contention, and support testability. By using pointers, the function is decoupled from the concrete implementation because it only depends on the interface type. Pointer passing also reduces the overhead of passing large objects, thereby reducing lock contention. Additionally, DI pattern makes it easy to write unit tests for functions using DI pattern since dependencies can be easily mocked.

See all articles