Quickly develop a PHP extension graphic tutorial _ php instance

Source: Internet
Author: User
Tags zts
Author: heiyeluren this article explains how to create a PHP5.2 environment extension (PHPExtension) in a very fast way, hoping to let friends who want to learn quickly learn about the production process in graphic mode. Requirement: for example, develop an extension called heiyeluren. in the extension, enter a string in the heiyeluren_test () function, 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: write code

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.
Echo"

";
print_r(get_loaded_extensions());
print_r(get_extension_funcs('heiyeluren'));
echo heiyeluren_test('My first php extension');
echo "
";
?>
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!

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.