新聞中心
php自定義擴(kuò)展(一)

成都創(chuàng)新互聯(lián)公司IDC提供業(yè)務(wù):綿陽(yáng)機(jī)房托管,成都服務(wù)器租用,綿陽(yáng)機(jī)房托管,重慶服務(wù)器租用等四川省內(nèi)主機(jī)托管與主機(jī)租用業(yè)務(wù);數(shù)據(jù)中心含:雙線(xiàn)機(jī)房,BGP機(jī)房,電信機(jī)房,移動(dòng)機(jī)房,聯(lián)通機(jī)房。
記得第一次寫(xiě)php擴(kuò)展是直接百度的,照著網(wǎng)上寫(xiě)完了一個(gè)擴(kuò)展,但自己不知所以然,先看看一個(gè)擴(kuò)展得基本步驟吧,然后再探討其中得原理。
利用源碼工具自動(dòng)生成擴(kuò)展目錄結(jié)構(gòu)
先進(jìn)入php源碼ext目錄下執(zhí)行下面命令
/www/test/php/php/bin/php ext_skel.php --ext helloworld cd helloworld
修改config.m4配置文件,就是現(xiàn)在寫(xiě)的擴(kuò)展是否用到外部依賴(lài),就配置--with-hello選項(xiàng),否則配置--enable-hello選項(xiàng),按照自己的需求把注釋去掉
dnl If your extension references something external, use 'with':
PHP_ARG_WITH([helloworld],
[for helloworld support],
[AS_HELP_STRING([--with-helloworld],
[Include helloworld support])])
dnl Otherwise use 'enable':擴(kuò)展功能書(shū)寫(xiě)
然后vim helloworld.c進(jìn)行擴(kuò)展功能代碼書(shū)寫(xiě)
先看下模塊結(jié)構(gòu)定義
zend_module_entry helloworld_module_entry = {
STANDARD_MODULE_HEADER,
"helloworld", /* Extension name */
helloworld_functions, /* zend_function_entry */
PHP_MINIT(helloworld), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(helloworld), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(helloworld), /* PHP_MINFO - Module info */
PHP_HELLOWORLD_VERSION, /* Version */
PHP_MODULE_GLOBALS(pib),
NULL,
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};功能函數(shù)名字集合
static const zend_function_entry helloworld_functions[] = {
PHP_FE(helloworld_test1, arginfo_helloworld_test1)
PHP_FE(helloworld_test2, arginfo_helloworld_test2)
PHP_FE_END
};真正的功能函數(shù)代碼
PHP_FUNCTION(helloworld_test2)
{
int argc = ZEND_NUM_ARGS();
char *messages = NULL;
size_t messages_len = 0;
char *context = NULL;
size_t context_len = 0;
zend_string *retval;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(messages, messages_len)
Z_PARAM_STRING(context, context_len)
ZEND_PARSE_PARAMETERS_END();
retval = strpprintf(0, "Hello %s test %s", messages, context);
RETURN_STR(retval);
}函數(shù)參數(shù)定義
ZEND_BEGIN_ARG_INFO(arginfo_helloworld_test2, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()編譯安裝
/www/test/php/php/bin/phpize ./configure --with-php-config=/www/test/php/php/bin/php-config make && make install
現(xiàn)在PHP的擴(kuò)展目錄中已經(jīng)有了helloworld.so這個(gè)文件,在php.ini中添加上擴(kuò)展的配置
extension = helloworld.so
然后就可以測(cè)試自己寫(xiě)的函數(shù)了helloworld_test2();完成了一個(gè)擴(kuò)展后,感覺(jué)自己也沒(méi)什么收獲,對(duì)為什么要這么寫(xiě)一點(diǎn)都不知道其中得原理,下編就來(lái)談?wù)撈渲械迷戆桑葟膒hp生命周期開(kāi)始介紹,見(jiàn)下篇。
網(wǎng)站題目:PHP如何自定義擴(kuò)展(一)之基本步驟
文章起源:http://www.fisionsoft.com.cn/article/djdsppo.html


咨詢(xún)
建站咨詢(xún)
