Introduction to PHP extension development experience

Source: Internet
Author: User
Tags zts
In the PHP development process, sometimes you need to select an extension method to improve the performance. This article briefly introduces how to develop a simple PHP extension to unlock the mysterious environment of PHP extension development: PHP 5.2.14 CentOS 5.5

Step 1: Create an extension skeleton

Cd php-5.2.14/ext
./Ext_skel-extname = laiwenhui

Step 2: modify compilation parameters

Cd php-5.2.14/ext/laiwenhui
Vi config. m4

Remove

PHP_ARG_ENABLE (laiwenhui, whether to enable laiwenhui support,

[-- Enable-laiwenhui Enable laiwenhui support])

Dnl before two rows

After modification:

The code is as follows:


Dnl Otherwise use enable:
PHP_ARG_ENABLE (laiwenhui, whether to enable laiwenhui support,
Dnl Make sure that the comment is aligned:
[-- Enable-laiwenhui Enable laiwenhui support])



Step 3: write code

Vim php_laiwenhui.h

Add PHP_FUNCTION (confirm_laiwenhui_compiled); followed by PHP_FUNCTION (test );

After adding:

PHP_FUNCTION (confirm_laiwenhui_compiled);/* For testing, remove later .*/
PHP_FUNCTION (test );

Then

Vim laiwenhui. c

Add PHP_FE (test, NULL) to PHP_FE (confirm_laiwenhui_compiled, NULL)

After adding:

The code is as follows:


Zend_function_entry laiwenhui_functions [] = {
PHP_FE (confirm_laiwenhui_compiled, NULL)/* For testing, remove later .*/
PHP_FE (test, NULL)/* For testing, remove later .*/
{NULL, NULL, NULL}/* Must be the last line in laiwenhui_functions [] */
};


Add the following code at the end of the file:

The code is as follows:


PHP_FUNCTION (test)
{
Char * arg = "This my first extention !";
Int len;
Char * strg;
Len = spprintf (& strg, 0, "% s \ n", arg );
RETURN_STRINGL (strg, len, 0 );
}


Step 4: compile the code

The code is as follows:


Cd php-5.2.6/ext/laiwenhui
/Opt/module/php/bin/phpize
./Configure-with-php-config =/opt/module/php/bin/php-config
Make
Make install


My PHP installation path is/opt/module/php
This will generate a file/opt/module/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui. so

Edit the PHP configuration file php. ini and add extensions:

Vim php. ini

Added extension = laiwenhui. so in the [PHP] module.

; Extension = php_zip.dll
Extension = laiwenhui. so

Modify extension_dir in the php. ini file to this directory:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613 /"

; Directory in which the loadable extensions (modules) reside.
Extension_dir = "/opt/module/php/lib/php/extensions/no-debug-non-zts-20060613 /"

Step 5: check the installation result

1. restart apache or php-fpm
2. check whether laiwenhui extensions are included in/opt/module/php/bin/php-m.
Step 6: execute the test code

Create test. php in the root directory of the website

Vim test. php

The code is as follows:

Echo test ();
?>

The result is: This my first extention!

If you can successfully complete the above steps, congratulations on completing the first extension.

The above is a simple step to expand PHP. For more information, see:

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.