A simple example of PHP extension in PHP 5.6 _php Tutorial

Source: Internet
Author: User

A simple example of PHP extension written in PHP version 5.6


This article mainly introduces the PHP 5.6 version of a simple example of writing a PHP extension, this article gives the extension implementation code, compile method, configuration method and use examples and other content, the need for friends can refer to the following

Sometimes in PHP itself does not meet the requirements of the API, you need to write the corresponding extension, the extension after the completion of the compilation, you can join their own development environment, to extend the functionality of PHP.

This implements a simple extension of the connection string and the join operation of the int type.

First, download the latest PHP source 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 the EXT/EXTSTRCAT/CONFIG.M4 and remove the previous comment (DNL) from the following line:

The code is as follows:


Php_arg_enable (Extstrcat, whether to ENABLE Extstrcat support,
Make sure, the comment is aligned:
[--enable-extstrcat enable EXTSTRCAT support])

Now, to edit ext/extstrcat/extstrcat.c, find the php_function (extstrcat) function, which means that the method named Extstrcat in the extension 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, ' Retstrlen ');
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 strings 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 under 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, add extension = extstrcat.so.
Restart PHP-FPM, run phpinfo () to see the new extstrcat extension.
Now to write a demo, test the PHP extension just a bit,

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.html www.bkjia.com true http://www.bkjia.com/PHPjc/971939.html techarticle A simple example of PHP extension written in PHP version 5.6 This article mainly introduces the PHP 5.6 version of the simple example of writing a PHP extension, this article gives the extension implementation code, compiling method, matching ...

  • Related Article

    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.