1. 程式人生 > >php擴展初探

php擴展初探

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擴展初探