PHP extension Development: The first extension

Source: Internet
Author: User

PHP extension Development: The first extension

Release time: December 14, 2011 Category: PHP

PHP extension development: Installing PHP

"POSIX timer: timer_settime ()"

In the previous article, "PHP Extension development: Installing PHP" We have successfully installed the PHP environment to develop PHP extensions, then create the first extension in the simplest and most straightforward way.

Let's first assume that the business scenario, which needs to have such an extension, provides a function called ccvita_string, whose primary function is to return a character. (This business scenario is too fake, let's just look at it) the corresponding PHP code might be:

function $str
      $result = "<a href=" ' ..;
     re Turn $result ;
}

The first step is to generate the code
PHP provides a tool ext_skel similar to the code generator for ease of development and can be found in the instructions.
First we create a file, Ccvita.skel, whose content is

String ccvita_string(string str)


is to tell Ext_skel this thing, we want to do the extension inside there is a function called ccvita_string. And then execute

cd mooenv< Span class= "pun" >/src/php- 5.3. 8/ext/
./ext_skel --extname= ccvita --proto=ccvita skel
CD ccvita/


At this point, the Ccvita code framework for this extension is already out.

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

DNL php_arg_with(Ccvita, for Ccvita support,
DNL Make sure, the comment is aligned:
DNL [ --with-ccvita Include ccvita support])


Revision changed to

php_arg_with (ccvita, for Ccvita support,
make sure that the COM ment is Aligned:
[  --< Span class= "KWD" >with-ccvita             include Ccvita support])

The third step is to realize the function
Modify Source CCVITA.C File
Find the Ccvita_string function modified to

Php_function(ccvita_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, "<a href=\"%.78s\ ">link</a > ", str);
Return_stringl(result, str_len, 0);
}

Fourth step, compiling the extension
After saving, start compiling

/usr/local< Span class= "pun" >/php/bin/ phpize
./configure ---php-config=< span class= "str" >/usr/local/php/bin/php-config
make

Fifth step, add the extension
At this point, all goes well, the extension is already in the modules/ccvita.so position. The following is to add this extension to PHP, so that we can invoke the PHP program.

CP Modules/ccvita. So /usr/local/PHP/ext/
Vim /usr/local/php/etc/php. Ini
Extension=/usr/local/PHP/ext/Ccvita. So #在php. ini file last added this line
Service PHP-fpm Restart #重启PHP服务
CP Ccvita. PHP /data/www/wwwroot/default/

Next you can access the ccvita.php file and test the extension. In the next article, there will be some more in-depth content.

Related code, can clone out on my GitHub, address: Https://github.com/KimiChen/PHP-EXT

PHP extension Development: The first extension

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.