


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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.