A simple example of writing a PHP extension in php 5.6 version, 5.6 example
Sometimes when PHP itself does not have an API that meets the needs, you need to write the corresponding extension yourself. After the extension is written, compile it and add it to your own development environment to expand the functions of PHP.
Here is a simple extension of the connection operation that connects strings and int numbers.
First, download the latest php source code installation package, enter the ext/ directory, and create a new extstrcat.def:
Copy code The code is as follows:
string extstrcat(string strarg, int intarg)
Then run:
Copy code The code is as follows:
./ext_skel --extname=extstrcat --proto=extstrcat.def
Modify ext/extstrcat/config.m4 and remove the comment (dnl) in front of the following line:
Copy code The code is as follows:
PHP_ARG_ENABLE(extstrcat, whether to enable extstrcat support,
Make sure that the comment is aligned:
[ --enable-extstrcat Enable extstrcat support])
At this time, edit ext/extstrcat/extstrcat.c and find the PHP_FUNCTION(extstrcat) function. This means that the method in the extension is called extstrcat. The method is implemented as follows:
Copy code The code is as follows:
PHP_FUNCTION(extstrcat)
{
char *strarg = NULL;
int argc = ZEND_NUM_ARGS();
int strarg_len;
long intarg;
char intargstr[10];
int retstrlen = 0;
char *retstr = NULL;
If (zend_parse_parameters(argc TSRMLS_CC, "sl", &strarg, &strarg_len, &intarg) == FAILURE)
return;
snprintf(intargstr, 9, "%d", intarg);
retstrlen = strarg_len + strlen(intargstr) + 1;
retstr = (char *)malloc(sizeof(char)* retstrlen);
Memset(retstr, '
strncat(retstr, strarg, strlen(strarg));
strncat(retstr, intargstr, strlen(intargstr));
RETURN_STRING(retstr, 1);
php_error(E_WARNING, "extstract: not yet implemented");
}
, where strarg and intarg are the corresponding two string and integer parameters.
The next thing to do is to compile the extension,
Copy code The code is as follows:
phpize
./configure --enable-extstrcat
make
After successful compilation, the extstrcat.so file will be generated in the ext/modules directory,
Copy code The code is as follows:
cp ./modules/extstrcat.so /usr/local/lib/php/extensions/no-debug-non-zts-20121212/
Modify php.ini and add extension = extstrcat.so.
Restart php-fpm and run phpinfo() to see that the extstrcat extension has been added.
Now let’s write a Demo and test the php extension just now,
Copy code The code is as follows:
if(!extension_loaded('extstrcat')) {
dl('extstrcat.' . PHP_SHLIB_SUFFIX);
}
$ret=extstrcat('testarg',1234);
echo $ret;
?>
, run the file under the command line and get testarg1234.
http://www.bkjia.com/PHPjc/945720.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/945720.htmlTechArticleA simple example of writing a PHP extension in php version 5.6. The 5.6 example sometimes does not have an API that meets the needs in php itself Sometimes, you need to write the corresponding extension yourself, and compile it after the extension is written...