Home > Backend Development > PHP Tutorial > Detailed explanation of how to write PHP extensions in C language_PHP tutorial

Detailed explanation of how to write PHP extensions in C language_PHP tutorial

WBOY
Release: 2016-07-21 15:07:03
Original
1028 people have browsed it

1: Predefine
in the home directory or any other directory. Write a file, such as caleng_module.def
. The content is the function name and parameters you want to define:
int a(int x,int y)
string b(string str,int n)

2: Go to the ext directory of the php source code directory
#cd /usr /local/php-5.4.0/ext/

Execute the command to generate the corresponding extension directory
#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def

3: Modify config.m4
Remove the dnl comment

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

4: Modification caleng_module.c

Copy code The code is as follows:

/* {{{ proto int a(int x, int y)
*/
PHP_FUNCTION(a)
{
int argc = ZEND_NUM_ARGS();
int x;
int y;
int z;
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)

return;
z=x+y;
RETURN_LONG(z);
}
/* }}} */
/* {{{ proto string b(string str, int n)
*/
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)
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 = '



6: Go to the corresponding extensions directory of php
As shown in the picture above
#cd /usr/local/php/lib/php/extensions /no-debug-non-zts-20100525/
There is a generated caleng_module.so file in the changed directory


7: Modify php.ini

php.ini if ​​you find it You can’t see the information typed from phpinfo()#cd /usr/local/php/lib/

php.ini Add extension information
extension=caleng_module.so

8: Restart Apache

# /usr/local/apache2/bin/apachectl restart

9: Check loading
/ usr/local/php/bin/php -m


10:PHP call

Copy code
The code is as follows:

echo a(1,2);

Output 3 means success!
==================================
The following is the original text
Under Linux Developing PHP extensions with C
1. First download the PHP source code package. Assume that the source code package directory is: /software/php-5.2.13
1. First download the PHP source code package. Assume that the source code package directory is: /software/php-5.2.13
#> cd /software/php-5.2.13/ext
2. Suppose we want to develop an extension named caleng_module, which contains two functions: a- -Process the addition of two integers and b-Process the repeated output of strings;
1. First write a function definition file. The function prototype suffix in the file is def, assuming: caleng_module.def
int a( int x, int y)
string b(string str, int n)
2. By extending the skeleton generator, the extension directory caleng_module
#> ./ext_skel -- extname=caleng_module --proto=caleng_module.def
3. Modify the configuration file: #> vim /software/php-5.2.13/ext/caleng_module/config.m4, remove the comment tag "dnl" from the following line , the modification is as follows:
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[ --enable-myfunctions    Enable myfunctions support])
4. Improve the functions of functions a and b: #> vim /software/php-5.2.13/ext/caleng_module/caleng_module.c
PHP_FUNCTION(a)
{
int x, y, z;

int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
return;
z = x + y ;
RETURN_LONG(z);
}
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)
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 = '



http://www.bkjia.com/PHPjc/327570.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/327570.html

TechArticle

1: Predefined in the home directory, or in any other directory, write a file, such as caleng_module.def. The content is The function name and parameters you want to define: int a(int x,int y) string...

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