Preliminary introduction to PHP extended development experience sharing

  • 2020-05-24 05:17:23
  • OfStack

Environment: PHP 5.2.14 CentOS 5.5

Step 1: build the extended skeleton

cd php-5.2.14/ext
. / ext_skel � extname = laiwenhui

Step 2: modify the build parameters

cd php-5.2.14/ext/laiwenhui
vi config.m4

To get rid of

PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,

[ --enable-laiwenhui Enable laiwenhui support])

The first two lines are dnl

After modification:
 
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 the code

vim php_laiwenhui.h

In PHP_FUNCTION (confirm_laiwenhui_compiled); Add a new line: PHP_FUNCTION(test);

After addition:

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

then

vim laiwenhui.c

After PHP_FE(confirm_laiwenhui_compiled, NULL) add PHP_FE(test, NULL)

After addition:
 
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 to the end of the file:
 
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
 
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 time will generate a file/opt/module/php/lib php/extensions/no - debug - non - zts - 20060613 / laiwenhui so

Edit the PHP configuration file php.ini, add the extension:

vim php.ini

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

;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 results

1. Restart apache or php-fpm
2. / opt/module/php/bin/php - m see whether include laiwenhui extension.
Step 6: execute the test code

Create test.php in the root directory of the site

vim test.php

The code looks like this

< ?php
echo test();
? >

The result is: This my first extention!

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

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

Related articles: