PHP Learning Notes PHP Expansion Development tutorial

Source: Internet
Author: User

Let's assume that we need such an extension to provide a function called tao_string, whose main function is to return a paragraph of characters. The corresponding PHP code may be like this:

The code is as follows Copy Code

function tao_string ($STR) {
$result = $str;
return $result;
}

The first step is to generate the code
PHP provides a tool ext_skel similar to the code generator in order to extend the convenience of development, and can refer to Php.net's official instructions (https://svn.php.net/repository/php/php-src/trunk/ Readme. Ext_skel).

Create a file Tao.skel, it's content is

String tao_string (String str)

is to tell Ext_skel this thing, the extension we're going to do has a function called tao_string. And then execute

The code is as follows Copy Code

CD mooenv/src/php-5.3.8/ext/
./ext_skel--extname=tao--proto=tao.skel
CD tao/

At this point, Tao's extended code frame is already out.

Step two, modify the configuration
Then modify the config.m4 file to delete the first dnl of lines 10, 11, 123, which is to

The code is as follows Copy Code

DNL Php_arg_with (TAO, for Tao Support,
DNL Make sure this comment is aligned:
DNL [--with-tao Include Tao support])

Amended to

Php_arg_with (TAO, for Tao Support,
Make sure this comment is aligned:
[--with-tao Include Tao support])

Step three, realize the function
Modify Source TAO.C File
Find the Tao_string This function is modified to

The code is as follows Copy Code

Php_function (tao_string)
{
char *str = NULL;
int argc = Zend_num_args ();
int str_len;
Char *result;

if (Zend_parse_parameters (argc tsrmls_cc, "s", &str, &str_len) = = failure)
Return

Str_len = spprintf (&result, 0, "Wo shi%.78s", str);
Return_stringl (result, str_len, 0);
}

Step fourth, compile the extension
After saving, start compiling

The code is as follows Copy Code

/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make

Step Fifth, add extension
At this point, if everything goes well, the extension is already in modules/tao.so. The following is to add this extension to PHP, so that we can invoke PHP program to.

The code is as follows Copy Code

CP modules/tao.so/usr/local/php/ext/
Vim/usr/local/php/etc/php.ini
extension=/usr/local/php/ext/tao.so #在php. ini file finally add this line
Service PHP-FPM Restart #重启PHP服务
CP tao.php/data/www/wwwroot/default/

You can then access the tao.php file and test the

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.