


Let's talk about dependency injection, containers and appearance patterns in framework development (Part 2)
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 'container.php'; //创建Facade类,实现以上三个功能 class Facade { //连接数据库 public static function connect(Container $container) { return $container->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:
<?php //数据库操作类 class Db { //数据库连接 public function connect() { return '数据库连接成功<br>'; } } //数据验证类 class Validate { //数据验证 public function check() { return '数据验证成功<br>'; } } //视图图 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 (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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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!

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 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.

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.

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.
