First, Swoole’s operation mode

What’s the difference between Swoole efficiency and traditional Web development? What’s the difference between Swoole’s asynchronous development mode and traditional LAMP/LNMP synchronous development mode?

1.1. Traditional Web development mode

The PHP Web development approach is LAMP/LNMP architecture, namely Linux, Nginx, Mysql and PHP. Here is an example of nginx:

When a request comes in, the Web Server forwards it to php-FPM, a process-pooled FastCGI service with a built-in PHP interpreter. The FPM is responsible for interpreting the response generated by executing the PHP file, which is eventually returned to the Web Server for presentation to the front end. PHP files implement a lot of business logic, including Mysql and Nosql access, calling third party applications and so on.

This structure of PHP-FPM and nginx works well enough, but because phP-FPM itself is a synchronous blocking process model that releases all resources (including a set of objects initialized by the framework) at the end of the request, the PHP process “idles” (create <–> destroy <–> create) and consumes a lot of CPU resources Source, resulting in a single machine throughput capacity is limited.

Each request processing process means a PHP file parsing, environment Settings and other unnecessary time-consuming operations in the PHP process to destroy the process, can not be used in THE PHP program to achieve performance optimization using connection pooling technology.

1.2. Swoole Operation mode

Compared with traditional architecture, swoole process model is characterized by its multithreaded Reactor model to deal with network requests, which makes it easy to deal with a large number of connections.

In addition, the advantages include:

Full asynchronous non – blocking, small occupation of resources, high efficiency program execution

The program only parses and loads the PHP file once to avoid repeated loading with each request

1.3. Disadvantages of using Swoole and traditional PHP development

1. Harder to get started. This requires developers to have a clearer understanding of how multiple processes operate

2. More prone to memory leaks. Be careful when handling global and static variables, because variables that are not cleaned up by the GC will last their entire life cycle and can easily run out of memory if not handled correctly. Under PHP-FPM, memory is completely freed as soon as the PHP code executes.

Second, annotation mechanism

Generally speaking, annotations are a parallel concept to annotations in programming Session. Before explaining annotations, we need to define the difference between annotations and annotations:

Comments: for programmers to see, to help understand the code, to explain the code, explain the role

Annotations: To an application, annotations often serve as a declaration and configuration of code, providing additional machine-available information to executable code that can affect program execution under certain circumstances.

Frameworks can provide a variety of additional functions to code based on this meta-information, and annotations are essentially just another way to understand that annotations are just another representation of configuration:

For example, it is more convenient to control permissions through annotations than through configuration files

For example, annotations are used to configure routes and scheduled tasks

Many of the existing Swoole-based frameworks are developed based on annotations, so we need to understand the annotation mechanism and then implement the annotations in code

Third, the container

3.1. What is a container?

A container is a giant factory that houses and manages the life cycle of objects and can decouple program dependencies.

3.2 Simple understanding of dependency injection through code

Coupling serious writing / * * * * * / * * class * * * * db * * {* * * * * * public static get_db function * * * * * * * * * * () {return * * * * * * new * * Mysqli (' 127.0.0.1 ', 'user', 'pass' and 'dbname, 3306); } } **class** **post** { **private** $db; **public** **function** __construct($db){// Suppose the database driver has changed? If write death only directly change the code * * $this - > db = * * * * new * * mysqli (' 127.0.0.1 ', 'user', 'pass', 'dbname, 3306); } **public** **function** **get_post**($id){ **return** **$this**->db->query('SELECT * FROM post WHERE id ='.$id); } } $post = **new** post(); $post->get_post(12); /* * dependency injection mode */ **<? php** **class** **db** { **public** **static** **function** **get_db**() { **return** **new** Mysqli (' 127.0.0.1 ', 'user', 'pass' and 'dbname, 3306); } } **class** **post** { **private** $db; **public** **function** **set_db**(db $db){ **$this**->db = $db; } **public** **function** **get_post**($id){ **return** **$this**->db->query(select xx from xxx); } } $post = **new** post(); $post->set_db( db::get_db() ); Get_db $POST -> get_POST (11);Copy the code

When there is no Ioc/DI container

When you have an IoC/DI container, the POST class no longer actively creates the DB class, as shown in the following figure:

Dependency injection: When an instance of class B is used in class A, the construction of the B object is not initialized in A class A method, but passed in as A class B object after it is initialized outside of class A. This process is called dependency injection. The required classes are passed in as parameters and are called dependency injection.

Inversion of Control (IoC of Control) refers to the transfer of Control of object creation. In the past, the initiative and timing of object creation were controlled by the IoC, but now the power is transferred to a third party, such as the IoC container, which is a factory for object creation. With the IOC container, the dependencies are changed, the old dependencies are gone, they all depend on the IOC container, and the relationship between them is established through the IOC container. Inversion of control means handing over control of the dependent class from active to passive.

3.3. Why does it make sense to use containers in Swoole?

Traditional PHP framework without permanent memory, so every request in all need to use a class is instantiated, each instantiation to apply for a memory, when the request processing after the need to release again, specific please see the first point, so we can at server startup when put the class instantiation prior to memory, cut into the object creation time.

A simple bean container

class BeanFactory{ private static $container=[]; public static function set(string $name,callable $func){ self::$container[$name]=$func; } public static function get(string $name){ if(isset(self::$container[$name])){ return (self::$container[$name])(); } return null; }}Copy the code