search
HomeBackend DevelopmentPHP TutorialLet's talk about dependency injection, containers and appearance patterns in framework development (middle)

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

We have solved the coupling problem between objects through dependency injection, but we have not fundamentally solved the problem;

Let us explain this more reasonable and excellent solution through the explanation of container technology. plan.

A container is actually a box, which can contain any service resources: classes, class instances, closures, functions, etc. Not only can the callee be placed inside,

even the main caller can Objects can also be placed inside. So containers are not mysterious. They have the same function as the containers we see every day, which are used to hold things.

At present, container technology has been widely used, and many excellent PHP developments are based on container technology to realize automatic loading of services.

For example: Laravel, ThinkPHP5.1, etc.

Container, also called service container, abbreviated as (IOC)

Basic idea: Just use it and simplify the calling of external objects to the greatest extent, similar to: [Plug and play] The idea

The basic implementation is divided into three steps:

1. Create a container and bind the class and the instantiation process of the class to the container (not limited to classes, but also interfaces or others)

2. Service registration, bind all tool classes that may be used to the container

3. Container dependency: or called dependent container, the container object is directly passed in when calling the work class. However, the instantiation of the tool class is completed by the container.

The following is the source code of the implementation:

<?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
{
//创建属性,用空数组初始化,该属性用来保存类与类的实例化方法
protected $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();
});
//测试:查看一下当前容器中的类实例
// var_dump($container->instance); die;
/******************************************************************************/
//三、容器依赖:将容器对象,以参数的方式注入到当前工作类中
//用户类:工作类
class User
{
//创建三个成员属性,用来保存本类所依赖的对象
// protected $db = null;
// protected $validate = null;
// protected $view = &#39;&#39;;
//这三个与外部对象对应的三个属性可以全部删除了,因为它们都已经事先注册到了容器中
//用户登录操作
// public function login(Db $db, Validate $validate, View $view)
//此时,只需从外部注入一个容器对象即可,Db,Validate和View实例方法全部封装到了容器中
public function login(Container $container)
{
//实例化Db类并调用connect()连接数据库
// $db = new Db();
// echo $db->connect();
echo $container->make(&#39;db&#39;)->connect();
//实例化Validate类并调用check()进行数据验证
// $validate = new Validate();
// echo $validate->check();
echo $container->make(&#39;validate&#39;)->check();
//实例化视图类并调用display()显示运行结果
// $view = new View();
echo $container->make(&#39;view&#39;)->display();
}
}
//在客户端完成工具类的实例化(即工具类实例化前移)
// $db = new Db();
// $validate = new Validate();
// $view = new View();
//现在注入过程就非常简单了,只需要从外部注入一个容器对象即可
//创建User类
$user = new User();
//调用User对象的login方法进行登录操作
// echo $user->login();
// 将该类依赖的外部对象以参数方式注入到当前方法中,当然,推荐以构造器方式注入最方便
echo &#39;<h3 id="用依赖容器进行解藕">用依赖容器进行解藕:</h3>&#39;;
// echo $user->login($db, $validate, $view);
//现在工作类中的login方法不需要再像对象依赖注入那样写三个对象了,只需要一个容器对象就可以了
echo $user->login($container);

In fact, the container mode can also use the appearance design mode to further simplify it.

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)

The above is the detailed content of Let's talk about dependency injection, containers and appearance patterns in framework development (middle). 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
如何使用PHP进行多语言框架开发?如何使用PHP进行多语言框架开发?May 13, 2023 am 08:09 AM

随着全球化的深入发展,越来越多的网站和应用程序需要支持多种语言。而PHP作为一种广泛应用于Web开发的编程语言,也需要支持多语言框架的开发。本文将介绍如何使用PHP进行多语言框架开发。一、什么是多语言框架首先,我们先来了解一下什么是多语言框架。多语言框架,顾名思义,就是一种可以支持多种语言的框架。在国际化和本地化设计中,多语言框架是必不可少的。它可以支持多种

Go语言框架开发中的异常处理与错误码设计Go语言框架开发中的异常处理与错误码设计Jun 05, 2023 pm 09:21 PM

随着互联网技术的不断发展,越来越多的企业开始使用Go语言进行开发。Go语言以其高效、稳定、易用的特点备受开发者的青睐。在企业级开发中,框架是不可或缺的一部分。因此,本文将介绍在Go语言框架开发中,如何进行异常处理与错误码设计。一、什么是异常处理在计算机编程中,异常处理指的是当程序运行过程中出现异常情况时,程序必须采取的措施。这些异常情况包括硬件故障、软件缺陷

如何在PHP中构建一个自定义框架?如何在PHP中构建一个自定义框架?Sep 05, 2023 pm 01:18 PM

如何在PHP中构建一个自定义框架?自定义框架是Web开发中的常见需求之一。通过构建自己的框架,开发者可以更好地满足项目的需求,并提高开发效率。本文将向您介绍如何在PHP中构建一个简单的自定义框架。一、框架结构一个典型的PHP框架应该包含以下几部分:路由器(Router):负责将URL映射到控制器和动作(方法)。控制器(Controller):接收并处理请求,

PHP8框架开发MVC:逐步指南PHP8框架开发MVC:逐步指南Sep 11, 2023 am 10:05 AM

PHP8框架开发MVC:逐步指南引言:MVC(Model-View-Controller)是一种常用的软件架构模式,用于将应用程序的逻辑、数据和用户界面分离。它提供了一种将应用程序分成三个不同组件的结构,以便更好地管理和维护代码。在本文中,我们将探讨如何使用PHP8框架来开发一个符合MVC模式的应用程序。第一步:理解MVC模式在开始开发MVC应用程序之前,我

PHP8框架开发MVC:初学者需要知道的重要概念和技巧PHP8框架开发MVC:初学者需要知道的重要概念和技巧Sep 11, 2023 am 09:43 AM

PHP8框架开发MVC:初学者需要知道的重要概念和技巧引言:随着互联网的快速发展,Web开发在当今的软件开发行业中扮演着重要的角色。PHP被广泛用于Web开发,并且有许多成熟的框架可以帮助开发人员更高效地构建应用程序。其中,MVC(Model-View-Controller)架构是最常见且广泛使用的模式之一。本文将介绍初学者在使用PHP8框架开发MVC应用程

学习Golang框架开发时的思考方式和注意事项学习Golang框架开发时的思考方式和注意事项Jun 03, 2023 am 08:41 AM

随着互联网的迅速发展,Golang语言作为一种新一代高效的编程语言,对于开发人员而言,已经成为不可或缺的一部分。Golang语言因其简洁的语法、高效的并发性能、良好的跨平台兼容性和自包含的静态链接库等优势已经受到了广泛的关注和应用。在这个过程中,框架的重要性也越来越显著。在学习Golang框架开发时,思考方式和注意事项也至关重要。一、框架的选择1.了解框架的

PHP8框架开发MVC模式:提高代码可维护性的最佳实践PHP8框架开发MVC模式:提高代码可维护性的最佳实践Sep 11, 2023 pm 08:12 PM

PHP8框架开发MVC模式:提高代码可维护性的最佳实践引言:随着互联网的快速发展,网站和Web应用程序的需求越来越多样化和复杂化。为了满足这种需求,开发人员需要使用高效可靠的框架来简化开发过程。在众多的PHP框架中,MVC(Model-View-Controller)模式是最常见且功能最强大的架构之一。本文将介绍PHP8框架开发MVC模式的最佳实践,以提高代

golang框架开发流程的最新进展golang框架开发流程的最新进展Jun 05, 2024 pm 02:52 PM

Golang框架开发流程的最新进展:最新框架:Gin、Echo和Fiber等最新框架提供高级特性和性能优化。实战案例:使用Gin构建RESTfulAPI:创建用户模型和控制器。使用Gin处理HTTP路由和请求解析。实现CRUD操作(创建、读取、更新、删除)。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool