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
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
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...