PHP怎么自定義擴展

本篇內(nèi)容主要講解“PHP怎么自定義擴展”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PHP怎么自定義擴展”吧!

成都創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設服務,提供成都網(wǎng)站建設、成都網(wǎng)站設計網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設計,自適應網(wǎng)站建設,網(wǎng)頁設計師打造企業(yè)風格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220

利用源碼工具自動生成擴展目錄結(jié)構(gòu)

先進入php源碼ext目錄下執(zhí)行下面命令

/www/test/php/php/bin/php ext_skel.php --ext helloworld
cd helloworld

修改config.m4配置文件,就是現(xiàn)在寫的擴展是否用到外部依賴,就配置--with-hello選項,否則配置--enable-hello選項,按照自己的需求把注釋去掉

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':

擴展功能書寫

然后vim  helloworld.c 進行擴展功能代碼書寫
先看下模塊結(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的擴展目錄中已經(jīng)有了helloworld.so這個文件,在php.ini中添加上擴展的配置

extension = helloworld.so

然后就可以測試自己寫的函數(shù)了helloworld_test2()

到此,相信大家對“PHP怎么自定義擴展”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

網(wǎng)頁標題:PHP怎么自定義擴展
本文網(wǎng)址:http://muchs.cn/article2/jcpeic.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司、動態(tài)網(wǎng)站品牌網(wǎng)站建設、品牌網(wǎng)站制作網(wǎng)站改版、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化