Php extension development tutorial for PHP learning notes. PHP extension development is not something that all developers can operate on. I will demonstrate the implementation process of PHP extension development. if you need it, please refer to it. Let's first assume that PHP extension development is not something that all developers can operate on. next I will demonstrate the PHP extension development implementation process. if you need it, please refer to it.
Let's assume that we need such an extension to provide a function named tao_string. its main function is to return a certain character. The corresponding PHP code may be like this:
The code is as follows: |
|
Function tao_string ($ str ){ $ Result = $ str; Return $ result; } |
Step 1: generate code
PHP provides an ext_skel tool similar to the code generator for the convenience of extension development. for details, see the official description of php.net (https://svn.php.net/repository/php/php-src/trunk/README.EXT_SKEL ).
Create a file tao. skel whose content is
String tao_string (string str)
This is to tell the ext_skel. the extension we want to do has a function named tao_string. Then execute
The code is as follows: |
|
Cd MooENV/src/php-5.3.8/ext/ ./Ext_skel -- extname = tao -- proto = tao. skel Cd tao/ |
At this time, tao's extended code framework has come out.
Step 2: modify the configuration
Modify the config. m4 file and delete the dnl at the beginning of lines 10, 11, and 12.
The code is as follows: |
|
Dnl PHP_ARG_WITH (tao, for tao support, Dnl Make sure that the comment is aligned: Dnl [-- with-tao Include tao support]) Change PHP_ARG_WITH (tao, for tao support, Make sure that the comment is aligned: [-- With-tao Include tao support]) |
Step 3: implement functions
Modify the source tao. c file
Find and change the tao_string function
The code is as follows: |
|
PHP_FUNCTION (tao_string) { Char * str = NULL; Int argc = ZEND_NUM_ARGS (); Int str_len; Char * result; If (zend_parse_parameters (argc TSRMLS_CC, "s", & str, & str_len) = FAILURE) Return; Str_len = spprintf (& result, 0, "wo shi %. 78 s", str ); RETURN_STRINGL (result, str_len, 0 ); } |
Step 4: compile the extension
After saving, start compiling
The code is as follows: |
|
/Usr/local/php/bin/phpize ./Configure -- with-php-config =/usr/local/php/bin/php-config Make |
Step 5: add an extension
At this time, if everything goes well, the extension is already in the position of modules/tao. so. The following is how to add this extension to PHP so that the PHP program can call it.
The code is as follows: |
|
Cp modules/tao. so/usr/local/php/ext/ Vim/usr/local/php/etc/php. ini Extension =/usr/local/php/ext/tao. so # add this line at the end of the php. ini file. Service php-fpm restart # restart the PHP service Cp tao. php/data/www/wwwroot/default/ |
Next, you can access the tao. php file and test it.
Bytes. Let's assume that we need...