A preliminary introduction to PHP extension development experience sharing_PHP tutorial

WBOY
Release: 2016-07-21 15:16:49
Original
835 people have browsed it

Environment: 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 in front of the two lines

After modification:

Copy code 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 a new line after PHP_FUNCTION(confirm_laiwenhui_compiled);: PHP_FUNCTION(test);

After adding:

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

Then

vim laiwenhui.c

in PHP_FE (confirm_laiwenhui_compiled, NULL) After adding PHP_FE(test, NULL)

after adding:
Copy the code 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:
Copy code The code is as follows:

PHP_FUNCTION(test)
{
char *arg = “This my first extention!”;
int len;
char *strg;
len = spprintf(&strg, 0, “%sn”, arg);
RETURN_STRINGL(strg, len, 0);
}

Step 4: Compile the code
Copy 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
At this time, a file /opt/module/php/lib/php/extensions will be generated /no-debug-non-zts-20060613/laiwenhui.so

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

vim php.ini

In [PHP ] Add under module: extension = laiwenhui.so

;extension=php_zip.dll
extension = laiwenhui.so

Change the 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 results

1. Restart apache or php-fpm
2. /opt/module/php/bin/php -m to see if the laiwenhui extension is included.
Step 6: Execute the test code

Create test.php in the root directory of the website

vim test.php

The code content is as follows

< ;?php
echo test();
?>

The result after execution is: This my first extention!

If you can successfully complete the above steps, congratulations! Completed the first extension.

The above are simple steps to extend PHP. For more information, please refer to:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325831.htmlTechArticleEnvironment: PHP 5.2.14 CentOS 5.5 Step 1: Create the extended skeleton cd php-5.2.14/ext. /ext_skel –extname=laiwenhui Step 2: Modify the compilation parameters cd php-5.2.14/ext/laiwenhui vi config....
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!