Compiling a simple example of PHP extension in php5.6 _ PHP Tutorial

Source: Internet
Author: User
In php5.6, compile a simple example of PHP extension. This article describes how to compile a simple PHP extension example in php5.6, this article provides a simple example of php extension in extension implementation code, compilation method, and configuration of PHP 5.6.

This article describes how to compile a simple php extension example in PHP 5.6. This article provides the extension implementation code, compilation method, configuration method, and use example. For more information, see

Sometimes, when php itself does not meet the needs of the api, you need to write the corresponding extensions by yourself. after the extensions are completed, you can compile them and add them to your development environment to expand the functions of php.

Here we implement a simple extension of the connection operation between the connection string and the int type.

First, download the latest php source code installation package, go to the ext/directory, and create 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) before 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])

Edit ext/extstrcat. c and find the PHP_FUNCTION (extstrcat). The method name in the extension is extstrcat. the implementation is 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, '\ 0', retstrlen );
Strncat (retstr, strarg, strlen (strarg ));
Strncat (retstr, intargstr, strlen (intargstr ));
RETURN_STRING (retstr, 1 );

Php_error (E_WARNING, "extstract: not yet implemented ");
}

Strarg and intarg are two corresponding string and integer parameters.
The next thing to do is compile the extension,

The code is as follows:


Phpize
./Configure -- enable-extstrcat
Make


After the compilation is successful, the extstrcat. so file is 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 (). you can see that the extstrcat extension is 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 on the command line to get testarg1234.

This article mainly introduces a simple example of PHP extension in php 5.6, this article provides the extension implementation code, compilation method, configuration...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.