Document directory
- 4.1. Execution of the program:
Explore the underlying PHP
1. What is PHP?
PHP refers to a complete system we can see from the outside. This may sound a bit confused, but it is not complicated (PHP4 internal structure ). From the functional aspect: We can divide it into three parts:
1. The Interpreter (Zend uses an engine) to analyze, translate, and execute the input code;
2. The functional part (PHP functions and extensions) is responsible for implementing various language functions (such as its functions );
3. The interface (SAPI) is responsible for session and other functions with the WEB server.
Zend includes the first part and the second part. The PHP kernel includes the second part and the third part. They are collectively called PHP packages. Zend constitutes the core of the language and also includes the implementation of some of the most basic PHP predefined functions. The PHP package (kernel) contains all modules that create significant features of the language itself.
(PHP internal structure)
From the content module: We can divide the system into four layers:
1) Zend engine: Zend is implemented in pure c and is the kernel part of php. It translates php code (a series of compilation processes such as lexical and Syntax Parsing) it can process and implement corresponding processing methods, implement basic data structures (such as hashtable and oo), allocate and manage memory, and provide corresponding api methods for external calls, is the core of everything, and all peripheral functions are implemented around zend.
2) Extensions Extension: around the zend engine, extensions provides various basic services in a component-based manner, and various common built-in functions (such as the array Series) and the standard library are all implemented through extension, you can also implement your own extension as needed to achieve function expansion and performance optimization (for example, the php middle layer being used by the post bar and Rich Text parsing are typical applications of extension ).
3) Sapi: The full name of Sapi is the Server Application Programming Interface, that is, the Server Application Programming Interface. sapi enables php to interact with peripheral data through a series of hook functions, this is a very elegant and successful php design. Through sapi, php itself is successfully decoupled from upper-layer applications. php can no longer consider how to be compatible with different applications, applications can also implement different processing methods based on their own characteristics.
4) Upper-layer applications: This is the php program we usually write, and various application modes are obtained through different sapi methods, for example, you can use webserver to implement web applications and run them in Script Mode under the command line.
(Php structure)
Its architectural philosophy: the engine (Zend) + extension (ext) mode: reduces internal coupling
Middle Layer (sapi): the communication interface between web server and php to isolate web server and php.
If php is a car
The car's framework is php itself, that is, we see a complete system outside.
Zend is a car engine)
The components below Ext are the wheels of a car.
Sapi can be seen as a highway, and vehicles can run on different types of Roads
The execution of a php program is that a car runs on the road.
Therefore, we need an engine with excellent performance + a proper wheel + a correct runway.
2. php Lifecycle
View: deep understanding of PHP underlying: PhP lifecycle: http://blog.csdn.net/hguisu/article/details/7377520
3. sapi
As mentioned above, sapi enables external applications to exchange data with php through a series of interfaces and implement specific processing methods based on different application features. Some common sapis include:
1) apache2handler: Apache is used as the webserver and the processing method used when running in mod_php mode. It is also the most widely used one.
2) cgi:This is another direct interaction method between webserver and php, that is, the well-known fastcgi protocol. fastcgi + php has been applied more and more recently this year, and it is also the only method supported by asynchronous webserver.
3) cli:Application Mode of command line call
Sapi is simple:
SAPI definition and main interface functions:
Struct _ sapi_module_struct {char * name; // name indicates char * pretty_name; // a better understanding of the name int (* startup) (struct _ sapi_module_struct * sapi_module ); // start function int (* shutdown) (struct _ sapi_module_struct * sapi_module); // disable method int (* activate) (TSRMLS_D); // activate int (* deactivate) (TSRMLS_D); // disable int (* ub_write) (const char * str, unsigned int str_length TSRMLS_DC); // unbuffered write void (* flush) (void * server_context); // flush struct stat * (* get_stat) (TSRMLS_D); // get uid char * (* getenv) (char * name, size_t name_len TSRMLS_DC ); // getenv void (* sapi_error) (int type, const char * error_msg ,...); /* error handler */int (* header_handler) (sapi_header_struct * sapi_header, sapi_header_op_enum op, sapi_headers_struct * sapi_headers TSRMLS_DC ); /* header handler * // * send headers handler */int (* send_headers) (sapi_headers_struct * sapi_headers TSRMLS_DC); void (* send_header) (sapi_header_struct * sapi_header, void * server_context TSRMLS_DC);/* send header handler */int (* read_post) (char * buffer, uint count_bytes TSRMLS_DC ); /* read POST data */char * (* read_cookies) (TSRMLS_D);/* read Cookies * // * register server variables */void (* register_server_variables) (zval * track_vars_array TSRMLS_DC); void (* log_message) (char * message);/* Log message */time_t (* get_request_time) (TSRMLS_D ); /* Request Time */void (* terminate_process) (TSRMLS_D);/* Child Terminate */char * php_ini_path_override; // overwrites the ini path ......};
Here we will introduce some of the main functions.
· Startup: Initialize PHP when it is called, such as CGI Mode. during startup, all extension is loaded and the initialization of the module is executed.
· Shutdown: Close the work when PHP is disabled
· Activate: Request Initialization
· Dectivate: The end of the request
· Ub_write: Specifies the data output mode, such as the apache2handler mode. Because PHP is a so of Apache, its output is also called
The ap_write function of Apache is used. In CGI mode, the system calls write.
· Sapi_error: error handling function
· Read_post: reads post data.
· Register_server_variables: register the environment variable to $ _ server. This variable is generally registered according to different protocol standards.
In the PHP source code, SAPI implements many interfaces: for example:
4. php script execution
SAPI is on the top of the PHP architecture, and the real script execution is completed by the Zend engine.
Currently, there are two types of languages:
Class 1: compiled languageFor example, c/c ++ java. Their common feature is that the source code must be compiled before running, and then the target file after compiling is run.
Second Language: interpreted language: PHP, Ruby, and Python. They need interpreters to execute these source code. In fact, these languages still need to be compiled. But they compile at runtime. for efficiency, they do not re-compile every execution, such as various PHP opcode cache extensions (such as APC Xcache ).
Note: Since PHP4 was released in 2000, PHP is not an explanatory language. When a PHP script is executed, the PHP source code is compiled by the Zend engine and becomes Zend opcodes. The code is stored in RAM. Then run opcodes to run the real script. Therefore, PHP is actually a compilation language like Java and C. Otherwise, its execution will be slow.
Let's see how PHP scripts are executed. For example, hello. php:
<?php$str = "Hello world!\n";echo $str;
Command Line execution: php hello. php
The output result is: Hello world!
But what does PHP/Zend do during script execution?
4.1. Execution of the program:
1) The execution file hello. php required by the php program. After the php program completes basic preparation, start the PHP and Zend engines and load the registered extension modules.
2) read the script file after initialization. The Zend engine performs lexical analysis and syntax analysis on the script. Then, the Zend engine compiles the script into an opcode code and finally executes the opcode code.
The php code execution process is as follows:
Php implements a typical dynamic language Execution Process: after a piece of code is obtained, the source code will be translated into commands (opcodes) after the lexical parsing and Syntax Parsing stages ), then the ZEND Virtual Machine sequentially executes these commands to complete the operation. PHP itself is implemented using c, so all the functions that are finally called are c functions. In fact, we can regard php as a software developed by c.
It is not difficult to see from the above description that the core of php Execution is a translated command, that is, opcode.
4.2. lexical analysis and syntax analysis
The interpreter consists of two parts:
1) read the source program and process the Language Structure
2) Stay in the language structure and generate the Target Program
Lex and Yacc can solve the first problem. Many programming languages use Lex/Yacc as the lexical syntax analysis generator. For example, PHP, Python, and Ruby are already MySql SQL languages.
Lex generates a lexical analyzer.
Yacc syntax analysis Generator
4. 3. opcode
PHP is built on Zend VM (Zend VM). PHP opcode is the command in zend vm, that is, Opcode is the most basic unit for php program execution.