PHP interpreter engine execution process

Source: Internet
Author: User
Tags sapi
PHP interpreter engine execution process ??? Here we will introduce the process of executing a PHP script inside the engine. taking cliSAPI as an example, we will briefly introduce the core part of the process, saving some initialization and cleaning operations .??? CLI (CommandLineInterface) is the PHP command line mode. Currently, this SAPI is installed by default. The PHP interpreter engine executes the process.

??? Here we will introduce the process of executing a PHP script inside the engine. we will take cli SAPI as an example to briefly introduce the core part of the process, saving some initialization and cleaning operations.
??? CLI (Command Line Interface) is the PHP Command Line mode. now this SAPI is installed by default. after installing PHP on the server, an executable file is usually generated, suppose the file is/usr/local/bin/php, then we can run a PHP script using the following command in SHELL:
/Usr/local/bin/php-f test. php
This command will execute the test. php script in the current directory. For the moment, we don't care about the specific content of test. php. we only care about the internal process of this execution.


????? The main source code file of CLI is in {PHPSRC}/sapi/cli/php_cli.c. The entire process is executed from the main () function in this file, and the entire function is relatively long, it can be divided into the following stages:
1: Parse command line parameters
2: initialize the environment
3: compile and execute PHP code
4: clean up the environment and return to exit


In the 1st phase, the resolution-f parameter is used to execute a php file, and the test. php file after-f is the file to be executed.
Here we will focus on how to execute php code in test. PHP in the 3rd phase.
The PHP script is executed through php_execute_script (& file_handle TSRMLS_CC). The function is defined in {PHPSRC}/main. c, and the prototype is:
PHPAPI int php_execute_script (zend_file_handle * primary_file TSRMLS_DC )?
The file_handle type is zend_file_handle. this is a zend encapsulation of the file handle, and the content in it is related to test. php.

Php_execute_script is the called zend_execute_scripts. This function is defined in {PHPSRC}/Zend/zend. c. the prototype is:
ZEND_API int zend_execute_scripts (int type TSRMLS_DC, zval ** retval, int file_count ,...)?
This function has variable parameters and can execute multiple php files at a time. The core of this function is to call zend_compile_file and zend_execute,

Zend_compile_file is a function pointer, which is declared in {PHPSRC}/Zend/zend_compile.c:
ZEND_API zend_op_array * (* zend_compile_file) (zend_file_handle * file_handle, int type TSRMLS_DC );? During engine initialization, the address of the compile_file function is assigned to zend_compile_file. the compile_file function is defined in {PHPSRC}/Zend/zend_policage_scanner.c. you can see that this function uses the zend_file_handle pointer, returns a pointer to zend_op_array.


Zend_execute is also a function pointer, which is declared in {PHPSRC}/Zend/zend_execute.c:
ZEND_API extern void (* zend_execute) (zend_op_array * op_array TSRMLS_DC );?
Similarly, when the engine is initialized, the address of the execute function is assigned to zend_execute. The definition of execute is {PHPSRC}/Zend/zend_vm_execute.h.
The declaration shows that zend_execute uses a pointer pointing to the zend_op_array structure as the parameter. this pointer is the return value of zend_compile_file. zend_execute starts to execute the op code in op_array. during the execution of op code, PHP functions are implemented.

?

The main task here is basically completed.

PS:

??? Why should I define zend_execute and zend_compile_file as function pointers?

??? When the engine is initialized (zend_startup), zend_execute is directed to the default execute, and zend_compile_file is directed to the default compile_file. We can rewrite zend_execute and zend_compile_file into other compilation and execution functions before actual compilation and execution, which leaves a hook for our extension engine, for example, a more famous extension vld (http://www.derickrethans.nl/projects.html#vld) for viewing PHP op code, this extension is in the hook function (PHP_RINIT_FUNCTION) initialized each request, replace zend_execute and zend_compile_file with your own vld_execute and vld_compile_file. These two functions encapsulate the original functions and add additional functions for outputting opcode information, because the engine initialization occurs before the module request initialization, and the module request initialization is before compilation and execution, this overwrite can achieve the goal.

?

?

Address: http://blog.csdn.net/phpkernel/article/details/5716342

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.