: This article mainly introduces PHP extension development 01: The first extension. if you are interested in the PHP Tutorial, refer to it. Let's assume that the business scenario requires such an extension to provide a function named ccvita_string. its main function is to return a certain character. (This is a false business scenario. let's take a look.) the corresponding PHP code may be as follows:
function ccvita_string($str){ $result = '$str.'">Link'; return$result;}
Step 1: generate code
For the convenience of extension development, PHP provides an ext_skel tool similar to the code generator. for details, see the description.
First, we create a file ccvita. skel whose content is
string ccvita_string(string str)
This is to tell the ext_skel. the extension we want to do has a function named ccvita_string. Then execute
cd MooENV/src/php-5.3.8/ext/./ext_skel --extname=ccvita --proto=ccvita.skelcd ccvita/
At this time, the ccvita 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.
dnl PHP_ARG_WITH(ccvita, for ccvita support,dnl Make sure that the comment is aligned:dnl [ --with-ccvita Include ccvita support])
Change
PHP_ARG_WITH(ccvita, for ccvita support,Make sure that the comment is aligned:[ --with-ccvita Include ccvita support])
Step 3: implement functions
Modify the ccvita. c file
Find and change the ccvita_string function
PHP_FUNCTION(ccvita_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, "Link", str); RETURN_STRINGL(result, str_len, 0); }
Step 4: compile the extension
After saving, start compiling
/usr/local/php/bin/phpize./configure --with-php-c/local/php/bin/php-configmake
Step 5: add an extension
At this time, if everything goes well, the extension is already in the position of modules/ccvita. so. The following is how to add this extension to PHP so that the PHP program can call it.
Cp modules/ccvita. so/usr/local/php/ext/vim/usr/local/php/etc/php. iniextension =/usr/local/php/ext/ccvita. so # in php. add this line of service php-fpm restart # restart the PHP service cp ccvita. php/data/www/wwwroot/default/
The related code can be cloned from my github at https://github.com/kimichen/php-extto download the ccvita.php file, which has been extended. The next article will cover more in-depth content.