Analyzing the triple realm of decoupling of PHP (a brief introduction to service containers)

Source: Internet
Author: User
Analyzing the triple realm of decoupling of PHP (a brief introduction to service containers)

Before reading this article you need to master: PHP syntax, object-oriented

In the process of completing the development of the whole software project, sometimes need to cooperate with many people, sometimes they can do their own independent, no matter what kind, along with the increase in code, write "Out of control", gradually "ugly interface, dirty realization", the project maintenance costs and difficulty rise, to the degree of difficulty to maintain, only reconstruction or redevelopment.

The first Realm

Hypothetical scenario: We need to write a processing class that can manipulate sessions, databases, and file systems at the same time. We might write it that way.

Boundary features: can be run, but seriously coupled

Class Db{public function DB ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class Filesystem{public function FileSystem ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class Session{public function Session ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class Writer{public function Write () {$db =new db, $filesystem =new filesystem (3,4); $session =new session (5,6);}} $writer =new writer (); $writer->write ();

Disadvantages of wording:

1. Constructs the object in the public function, once involves the change as the database parameter, the modification will have the very heavy work

2. The person responsible for designing the writer class needs to be familiar with various APIs such as DB

Is there a way to reduce the coupling degree?

Second realm (parametric dependency)

Hypothetical scenario: The database address needs to be changed frequently because of the customer, and there are a lot of calls to the DB Class (if there are dozens of), I hope you don't have to modify the code of these classes even if you change the database address.

Class Db{public function DB ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class Filesystem{public function FileSystem ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class Session{public function Session ($arg 1, $arg 2) {echo ' constructed! '. Php_eol;}} Class writer{protected $_db;protected $_filesystem;protected $_session;public function Set ($db, $filesystem, $session) {$this->_db= $db; $this->_filesystem= $filesystem; $this->_session= $session;} Public Function Write () {}} $db =new db, $filesystem =new filesystem (3,4), $session =new session (5,6); $writer =new Writer (); $writer->set ($db, $filesystem, $session); $writer->write ();

Although the construction of the DB class is moved to the client, and once the modification is involved, the workload is greatly reduced, but the new problem comes: In order to create a writer class, we need to first create a good DB class, FileSystem class, which is responsible for the writer class, the requirements are very high, He needs to look at a lot of other classes of documents, create them (and possibly initialize them), and then create the writer variables that he wants.

Therefore, we hope that there is a better way of writing, so that the writer class, with a more efficient interface, you can create and invoke the class he wants, even the parameters are not filled.

Third Realm (IOC container)

After the first two realms, we hope to add the following benefits:

1. I hope that the DB class, session class, FileSystem class "Take to be used", without each tedious initialization, such as write $db=new db (ARG1,ARG2), such statements.

2. You want the objects of type db to be "global" and can be called at any time during the entire program run.

3. Programmers who call a type such as db do not have to know too much about this class, and can even create an object with an alias for a string.

The IOC container, which is able to achieve the above objectives, can simply look at the IOC container as a global variable and bind the string and constructor with an associative array.

Let's first implement a container class

Class Container{public $bindings;p ublic function bind ($abstract, $concrete) {$this->bindings[$abstract]= $concrete ;} Public function make ($abstract, $parameters =[]) {return Call_user_func_array ($this->bindings[$abstract],$ parameters);}}

Service Registration (binding)

$container =new container (); $container->bind (' db ', function ($arg 1, $arg 2) {return new db ($arg 1, $arg 2);}); $container->bind (' Session ', function ($arg 1, $arg 2) {return new session ($arg 1, $arg 2);}); $container->bind (' FS ', function ($arg 1, $arg 2) {return new FileSystem ($arg 1, $arg 2);});

Container dependency

class writer{protected $_db;protected $_filesystem;protected $_session; Protected $container;p ublic function Writer (container $container) {$this->_db= $container->make (' db ', [up]); $ this->_filesystem= $container->make (' Session ', [3,4]), $this->_session= $container->make (' FS ', [5,6]);}} $writer =new writer ($container); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.