Quickly develop a PHP Extension

Source: Internet
Author: User
Tags php source code zts

Introduction: This is a detailed page for rapid development of PHP extensions. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 340016 'scrolling = 'no'> This article explains how to create an extension of the PHP 5.2 environment (PhP extension) very quickly ), I hope that the production process can be understood by anyone who wants to learn quickly in the form of text and text.

Requirements:For example, if you develop an extension called heiyeluren, input a string using the heiyeluren_test () function in the extension, and the function returns: Your input string: XXXXX.
Requirements:Understand C/C ++ programming and be familiar with PHP programming
Environment:Download a PHP corresponding version of the source code, I here is the php-5.2.6, the first normal installation of PHP, assuming that our PHP installed in the/usr/local/PHP Directory, source code in/root/soft/PHP/php-5.2.6/, now start!

Step 1: generate an extension framework


CDS/root/soft/PHP/php-5.2.6/EXT
./Ext_skel -- extname = heiyeluren
CD/root/soft/PHP/php-5.2.6/EXT/heiyeluren
VI config. M4
After opening the file, remove DNL and obtain the following information:
Php_arg_enable (heiyeluren, whether to enable heiyeluren support,
[-- Enable-heiyeluren enable heiyeluren support])
Save and exit.
(Figure 01)

Step 2: CompileCode

VI php_heiyeluren.h
Find: php_function (confirm_heiyeluren_compiled); and add a new row:
Php_function (heiyeluren_test );
Save and exit.
(Figure 02)

VI heiyeluren. c
Add our function to the array, find zend_function_entry heiyeluren_functions [], and add:
Php_fe (heiyeluren, null)
(Figure 03)

Add the following code to the end of the heiyeluren. c file:
Php_function (heiyeluren_test)
{
Char * Arg = NULL;
Int arg_len, Len;
Char * strg;

If (zend_parse_parameters (zend_num_args () tsrmls_cc, "S", & Arg, & arg_len) = failure ){
Return;
}

Len = spprintf (& strg, 0, "Your input string: % s/n", ARG );
Return_stringl (strg, Len, 0 );
}
Save and exit.
(Figure 04)

Step 3: Compile and install

CD/root/soft/PHP/php-5.2.6/EXT/heiyeluren
/Usr/local/PHP/bin/phpize
./Configure -- With-PHP-Config =/usr/local/PHP/bin/PHP-config
Make
Make Test
Make install

Now check if there is a/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20060613/heiyeluren. So
Edit PHP. ini and add the extension:
VI/usr/local/PHP/lib/PHP. ini
Add the following in the [PHP] module:
Extension = heiyeluren. So
Save and exit.
(Figure 05)

Note: If you do not have an extension file directory or the installation reports an error, you can create this directory on your own, copy the extension to the directory, and remember to copy the PHP. change extension_dir In the INI file to this directory:
Extension_dir = "/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20060613 /"
(Figure 06)

Step 4: Check the installation result
Now let's see if the module has been loaded:
/Usr/local/PHP/bin/PHP-M, which should be printed out as follows:
[PHP modules]
...
Heiyeluren
...
[Zend modules]

Restart Apache and output phpinfo (). You can see:
Heiyeluren
Heiyeluren support enabled
(Figure 07)

Check whether the function exists and is called. Create heiyeluren. php In the web directory.
<? PHP
Echo "<PRE> ";
Print_r (get_loaded_extensions ());
Print_r (get_extension_funcs ('heiyeluren '));
Echo heiyeluren_test ('My first PHP extension ');
Echo "</PRE> ";
?>
When accessing Apache, you can see:
Array
(
...
[33] => heiyeluren
)
Array
(
[0] => confirm_heiyeluren_compiled
[1] => heiyeluren_test
)
Your input string: heiyeluren
(Figure 08)

Extension created successfully!

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/340016.html pageno: 7.

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.