当前位置:网站首页>Overview of PHP architecture and principle

Overview of PHP architecture and principle

2021-01-23 16:23:53 Programmer a mu

PHP Characteristics

Multi process model
PHP It's designed in a multi process model , The advantage is that requests don't interfere with each other , A request failure will not affect other processes , As a tool set used only for personal websites at the beginning, there is nothing wrong with this design , With PHP It's getting bigger , It is obviously inappropriate to increase the number of visits , Because the cost of starting a process is not cost-effective for massive requests , So now PHP It's basically running on PHP-FPM Under the management of , This is a PHP Process Manager , It's resident in memory to start some PHP Process standby , Assign a process to process when the request enters ,PHP Recycle the process after it is processed , But it doesn't destroy the process , This makes PHP It can also cope with high traffic access requests .

Now, of course, there are PHP Multithreading solutions and coroutine based solutions , such as swoole Give Way PHP More efficient processing WEB request .

Weak type
And JAVA、C/C++ Different ,PHP It's a kind of language , A variable does not need to be typed at the moment it is declared , At run time, the type will change either explicitly or implicitly , This is also PHP Rapid development and Application 、 One of the reasons for convenience .

other
Zend engine + Ext Expand The model reduces the internal coupling , It is convenient for PHP Add function and remove function .
Grammar is simple , There are not many mandatory norms , In terms of programming style, you can use programs 、 It can also be developed in an object-oriented way , Of course, the function can also .

PHP The architecture of


To the present PHP Mainstream version PHP7 and PHP5 The architecture is as shown in the figure above , It is mainly composed of four layers , From bottom to top is Zend engine 、Extensions Expand 、SAPI Interface 、 Upper application .

Zend engine
Zend Engine is PHP4 Join in later PHP Of , It's about the original PHP Rewriting of the interpreter , Overall use C Language development , That is to say, we can PHP It is understood as using C Write a programming language software , The role of the engine is to PHP The code translates into a language called opcode Middle language of , It is similar to JAVA Of ByteCode( Bytecode ).
Engine pair PHP The code performs four steps :

  1. Lexical analysis Scanning(Lexing), take PHP Code to language snippets (Tokens).
  2. analysis Parsing, take Tokens Convert to simple and meaningful expressions .
  3. compile Compilation, Compile the expression into Opcode.
  4. perform Execution, Sequential execution Opcode, One at a time , In order to realize the PHP The function expressed by the code .

APC、Opchche These extensions can make Opcode Cache to speed up PHP The speed of the application , Using them, you can omit the first three steps when the request comes again .
The engine also implements the basic data structure 、 Memory allocation and management , Corresponding API Method for external calls .

Extensions Expand
Common built-in functions 、 The standard library is all through extension To achieve , These are called PHP The core extension of , Users can also install it according to their own requirements PHP An extension of .

SAPI
SAPI yes Server Application Programming Interface Abbreviation , Chinese for the server application programming interface , It uses a series of hook functions to make PHP You can exchange data with the periphery ,SAPI Namely PHP And the agent of the external environment , It abstracts the external environment , For internal PHP Provide a set of fixed , Unified interface , bring PHP Self implementation can be independent of the complex external environment , Maintain a certain degree of Independence .
adopt SAPI The decoupling ,PHP We can no longer consider how to be compatible for different applications , The application itself can also implement different processing methods according to its own characteristics .

Upper application
Written by a programmer PHP Program , Whether it's Web Application or Cli The applications that run in this mode are all upper level applications ,PHP The main job of programmers is to write them .

summary
When you understand this, you will understand why PHP The founder of the company would say PHP Development doesn't need a framework , because PHP The design itself is a framework like thing , If you use the car metaphor PHP It's the skeleton of the car ,Zend It's the engine ,Ext It's a wheel , Steering wheel and other car components ,SAPI That's the way .

For these things , Maybe some people think it's a little useless , Because a lot of people are paranoid that they are just drivers , Why do drivers need to know how the car runs , Even more scornful of knowing how to build a car .
Until the translation Laravel When the document ,Laravel I was inspired by the author of , I also send these words to you who read this article :
Use 「 Real world 」 Any tool in , If you understand how this tool works , Then you will be more comfortable with this tool . So is application development . When you understand how your development tools work , You'll be able to use them .

I hope the above will help you , quite a lot PHPer There are always some problems and bottlenecks in the advanced stage , There is no sense of direction in the business code , I don't know where to start to improve , I've compiled some information about it , Including but not limited to : Distributed architecture 、 Highly scalable 、 High performance 、 High concurrency 、 Server performance tuning 、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql Optimize 、shell Script 、Docker、 Microservices 、Nginx If you need advanced advanced dry goods, you can share them for free , need

Down and out programmer :【 The year-end benefits are known 】10 Architects share PHP Advanced architecture information , We can all 30Kzhuanlan.zhihu.com

or person Follow our Zhihu column below
I hope the above will help you , quite a lot PHPer There are always some problems and bottlenecks in the advanced stage , There is no sense of direction in the business code , I don't know where to start to improve , I've compiled some information about it , Including but not limited to : Distributed architecture 、 Highly scalable 、 High performance 、 High concurrency 、 Server performance tuning 、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql Optimize 、shell Script 、Docker、 Microservices 、Nginx If you need advanced advanced dry goods, you can share them for free , You can join if you need My official group Click here to .

版权声明
本文为[Programmer a mu]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123162222103r.html