Home >Backend Development >PHP Tutorial >A simple example of writing a PHP extension in PHP 5.6 version_PHP Tutorial

A simple example of writing a PHP extension in PHP 5.6 version_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:43766browse

A simple example of writing a PHP extension in php 5.6 version

This article mainly introduces a simple example of writing a PHP extension in php 5.6 version. This article gives the extension implementation code , compilation methods, configuration methods and usage examples, friends in need can refer to it

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 and compiled, you can add it to your own development environment and 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:

The code is as follows:


string extstrcat(string strarg, int intarg)
Then run:

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:

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:

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,

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.

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,

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/971939.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971939.htmlTechArticleA simple example of writing a PHP extension in php 5.6 version This article mainly introduces writing a PHP extension in php 5.6 version A simple example of extension. This article gives the extension implementation code, compilation method, configuration...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn