Home  >  Article  >  Backend Development  >  PHP extension development-code example sharing for LINUX environment

PHP extension development-code example sharing for LINUX environment

黄舟
黄舟Original
2017-03-22 10:05:001256browse


LINUX The steps to develop PHP extensions in the environment are as follows:

1. Download the PHP source code and unzip it. My unzipped directory is: /root /lamp/php-5.5.37

2. cd to the /root/lamp/php-5.5.37/ext directory and create the file test_extension.def file

int a(int x, int y)string b(string str, int n)

3. Through extension FrameworkGeneratorGenerate framework directory:
ext_skel –extname=test_extension –proto=test_extension.def
The successful generation results are as follows:

Creating directory test_extension
awk: /root/lamp/php-5.5.37/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .svnignore test_extension.c php_test_extension.h CREDITS EXPERIMENTAL tests/001.phpt test_extension.
php [done].To use your new extension, you will have to execute the following steps:
1.  $ cd ..
2.  $ vi ext/test_extension/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-test_extension
5.  $ make
6.  $ ./sapi/cli/php -f ext/test_extension/test_extension.php
7.  $ vi ext/test_extension/test_extension.c
8.  $ make
Repeat steps 3-6 until you are satisfied with ext/test_extension/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

4. Switch to the generated framework directory: cd test_extension
5. Modify the configuration file config.m4 and remove the first lines 10, 11 and 12. The dnl is as follows

PHP_ARG_WITH(test_extension, for test_extension support,
Make sure that the comment is aligned:
[  --with-test_extension             Include test_extension support])

6. To implement the functions of functiona and b, vi test_extension.c, the modified functions a and b are as follows

PHP_FUNCTION(a)
{        
int argc = ZEND_NUM_ARGS();        
long x;        
long y;        
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
        {
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        RETURN_LONG(x + y);
}

PHP_FUNCTION(b)
{        
char *str = NULL;        
int argc = ZEND_NUM_ARGS();        
int str_len;        
long n;        
char *result;        
char *ptr;        
int result_length;        
if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        {
        
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        result_length = str_len * n;
        result = (char *) emalloc(result_length + 1);
        ptr = result;        while (n--) {
                memcpy(ptr, str, str_len);
                ptr += str_len;
        }
        *ptr = '/0';
        RETURN_STRINGL(result, result_length, 0);

}

7.test_extension directory Execute under: /usr/local/bin/phpize

Configuring for:
PHP Api Version:         
20121113Zend Module Api No:      
20121212Zend Extension Api No:   
220121212

8. Configuration: ./configure –with-php-config=/usr/local/bin/php-config
9. Compile: make
10. Installation: make install
After the installation is completed, test_extension.so will be generated under /usr/local/lib/php/extensions/no-debug-zts-20121212/

11. Modify php .in, add: extension=test_extension.so

The above is the detailed content of PHP extension development-code example sharing for LINUX environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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