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 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: |
Copy code |
|
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: |
Copy code |
|
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: |
Copy code |
|
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: |
Copy code |
|
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: |
Copy code |
|
/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: |
Copy code |
|
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.