Why should I use C extension C for static compilation? The execution efficiency is much higher than that of PHP code. Using C for the same operation code, the performance will be improved by hundreds of times than PHP. IO operations such as CURL, because the time consumed is mainly on IOWait, C extension has no obvious advantage. In addition, the C extension is loaded when the process is started. The PHP code can only operate on data in the Request lifecycle.
Why should I use C extension C for static compilation? The execution efficiency is much higher than that of PHP code. Using C for the same operation code, the performance will be improved by hundreds of times than PHP. IO operations such as CURL, because the time consumed is mainly on IOWait, C extension has no obvious advantage. In addition, the C extension is loaded when the process is started. The PHP code can only operate on data in the Request lifecycle.
Why use C extension?
C is statically compiled, and the execution efficiency is much higher than that of PHP code. Using C for the same operation code, the performance will be improved by hundreds of times than PHP. IO operations such as CURL, because the time consumed is mainly on IOWait, C extension has no obvious advantage.
In addition, the C extension is loaded when the process is started. The PHP code can only operate the data in the Request lifecycle, and the C extension can operate on a wider range.
Step 1
Download PHP source code, such as php-5.4.16. Decompress the package and go to the php-5.4.16 \ ext directory. Enter./ext_skel-extname = myext. myext is the extension name. After execution, the myext directory is generated.
Ext_skel is a tool officially provided by PHP for generating php extension skeleton code.
Cd myext. You can see several files, such as php_myext.h, myext. c, and config. m4. Config. m4 is the configuration file of the AutoConf tool. It is used to modify various compilation options.
Step 2
Modify config. m4 and set
dnl PHP_ARG_WITH(myext, for myext support,dnl Make sure that the comment is aligned:dnl [ --with-myext Include myext support])
Change
PHP_ARG_WITH(myext, for myext support,[ --with-myext Include myext support])
There is also a-enable-myext below, which indicates compiling to the php kernel. With is loaded as a dynamic link library.
Step 3
Modify php_myext.h and see PHP_FUNCTION (confirm_myext_compiled). Here is the declaration part of the extended function. You can add a line of PHP_FUNCTION (myext_helloworld), which declares an extension function of myext_helloworld.
Modify myext. c, which is the implementation part of the extension function.
const zend_function_entry myext_functions[] = { PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */ PHP_FE(myext_helloworld, NULL) PHP_FE_END /* Must be the last line in myext_functions[] */};
This code registers the function pointer to the Zend engine and adds a line of PHP_FE (myext_helloworld, NULL) (with a semicolon not followed ).
Step 4
Add the Execution Code of myext_helloworld to the end of myext. c.
PHP_FUNCTION(myext_helloworld){ char *arg = NULL;int arg_len, len;char *strg;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {return;}php_printf("Hello World!\n");RETRUN_TRUE;}
Zend_parse_parameters is used to accept the input parameters of PHP, And the RETURN_XXX macro is used to return data to PHP.
Step 5
Execute phpize,./configure, make, and make install in the myext directory in sequence. Modify php. ini and add extension = myext. so.
Run php-r "myext_helloworld ('test');" to output hello world!