A simple example of PHP extension written in PHP 5.6, 5.6 Example _php tutorial

Source: Internet
Author: User

A simple example of PHP extension written in PHP 5.6, 5.6 Example


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:
Copy the Code code as follows:
String Extstrcat (string strarg, int intarg)

Then run:
Copy the Code code 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:
Copy the Code code as follows:
Php_arg_enable (Extstrcat, whether to ENABLE Extstrcat support,
Make sure, the comment is aligned:
[--enable-extstrcat enable EXTSTRCAT support])

At this point to edit ext/extstrcat/extstrcat.c, find the php_function (extstrcat) function, which means that the method name in the extension is Extstrcat, the method is implemented as follows:
Copy the 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;< br>
snprintf (intargstr, 9, "%d", intarg);
Retstrlen = Strarg_len + strlen (intargstr) + 1;
Retstr = (char *) MA Lloc (sizeof (char) * retstrlen);
memset (retstr, ' n ', 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,
Copy the Code code as follows:
Phpize
./configure--enable-extstrcat
Make

After the compilation is successful, the extstrcat.so file is generated under the Ext/modules directory,

Copy the Code code 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,
Copy the Code code as follows:
<?php
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.html www.bkjia.com true http://www.bkjia.com/PHPjc/945720.html techarticle PHP 5.6 Version of a simple example of a PHP extension, 5.6 Examples sometimes in PHP itself does not meet the requirements of the API, it is necessary to write their own extension, the extension after the completion of the compilation ...

  • 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.