標籤:php 擴充
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()
?>
本文出自 “Linux營運” 部落格,請務必保留此出處http://2853725.blog.51cto.com/2843725/1948992
php擴充初探