1.首先生成扩展框架结构
Cd /alidata/tmp/php-5.6.23/ext
./ext_skel --extname=myext --proto=myext.fun
1)此时会生成相关文件
Configure config.h include myext.c config.m4
2)主要修改文件
Config.m4
PHP_ARG_WITH(myext, for myext support,
Make sure that the comment is aligned:
[ --with-myext Include myext support])
dnl Otherwise use enable:
PHP_ARG_ENABLE(myext, whether to enable myext support,
Make sure that the comment is aligned:
[ --enable-myext Enable myext support])
myext.c
1)头部文件
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_myext.h"
#include<stdio.h>
#include<malloc.h> //内存管理调用
2)变量定义
static int le_myext;
3)定义函数
const zend_function_entry myext_functions[] = {
PHP_FE(myext, NULL) /* For testing, remove later. */
PHP_FE(myext_fork,NULL)
PHP_FE(myext_list,NULL)
//PHP_PE(myext_insert,LinkList* list, LinkListNode* node, int pos)
//PHP_PE(myext_get,LinkList* list, int pos)
//PHP_PE(myext_create,LinkList*)
//PHP_PE(myext_destroy,LinkList* list)
PHP_FE_END /* Must be the last line in myext_functions[] */
};
4)zend模块定义
zend_module_entry myext_module_entry = {
STANDARD_MODULE_HEADER,
"myext",
myext_functions, //代表加载定义的函数@标识
PHP_MINIT(myext),
PHP_MSHUTDOWN(myext),
PHP_RINIT(myext), /* Replace with NULL if there‘s nothing to do at request start */
PHP_RSHUTDOWN(myext), /* Replace with NULL if there‘s nothing to do at request end */
PHP_MINFO(myext),
PHP_MYEXT_VERSION,
STANDARD_MODULE_PROPERTIES
};
5)实现导出函数
PHP_FUNCTION(myext_list)
{
long lptr;
double dptr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {
return;
}
zend_printf("lptr value ad %ld",lptr);
zend_printf("dptr value ad %ld",dptr);
}
6)接受用户传递的参数
6.1)取得参数个数
ZEND_NUM_ARGS() TSRMLS_CC //zend_api.h中有定义
6.2)取得实体参数
//定义变量
long lptr;
double dptr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {
return;
}
&lptr,:表示接受的参数
&dptr:表示接受的参数
//参数打印
zend_printf("lptr value ad %ld",lptr);
zend_printf("dptr value ad %ld",dptr);
7)完整的导出函数实现
PHP_FUNCTION(myext_list)
{
long lptr;
double dptr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &lptr, &dptr) == FAILURE) {
return;
}
zend_printf("lptr value ad %ld",lptr);
zend_printf("dptr value ad %ld",dptr);
}
2.编辑扩展
/alidata/server/php-5.6.23/bin/phpize 压缩扩展
./configure --with-php-config=/alidata/server/php-5.6.23/bin/php-config 编译安装扩展
/alidata/server/php-5.6.23/bin/php -m 显示已经安装的扩展
3.修改php.ini
[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp
extension=swoole.so
extension=myext.so
extension=php_list.so
4.php调用
<?php
myext_list()
?>