<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                一般我們自定義的擴展都是以動態編譯方式生成動態鏈接庫.so文件,通過在php.ini讀取擴展信息,php會將動態鏈接庫加載到內存;php的擴展可以分為兩類,一種是php擴展,需要實現zend_module_entry結構體,在php.ini中通過extension=xx.so加載擴展;一種是zend擴展,需要實現zend_extension,在php.ini通過zend_extension=xx.so加載擴展 以下主要記錄php擴展:zend_module_entry定義了擴展的全部信息:擴展名、擴展版本、擴展提供的函數列表以及PHP四個執行階段的hook函數等 ### 第一步:生成骨架 位于源代碼的ext目錄下,執行 ```c ./ext_skel --extname=hello --proto=hello.def ``` --proto用來指定函數原型,例如 ```c // hello.def vi hello.def int wcl(string filename) // 添加wcl函數原型,wcl用于統計文件行數 ``` 函數原型對應會生成如下代碼: ```c PHP_FUNCTION(wcl) { char *filename = NULL; int argc = ZEND_NUM_ARGS(); size_t filename_len; if (zend_parse_parameters(argc, "s", &filename, &filename_len) == FAILURE) return; php_error(E_WARNING, "wcl: not yet implemented"); } ``` PHP_FUNCTION(wcl)即我們要實現功能代碼,如果不指定函數原型,需要自己手動添加該函數 ### 第二步:config.m4配置編譯信息 config.m4主要用于配置編譯參數(PHP_ARG_ENABLE)和設置擴展的源文件(PHP_NEW_EXTENSION) ```c PHP_ARG_ENABLE(hello, whether to enable hello support, dnl Make sure that the comment is aligned: [ --enable-hello Enable hello support]) ``` PHP_ARG_ENABLE對應編譯時的--enable,表示是否啟用擴展,這里將dnl注釋去掉 ```c PHP_NEW_EXTENSION(hello, hello.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) ``` - 第一個參數表示擴展名稱 - 第二個參數表示擴展源文件列表,如果有多個源文件,需要以空格隔開;換行時加上反斜杠“\” ### 第三步:實現功能 在ext/hello/hello.c文件中,PHP_FUNCTION實現相應的功能 ```c /* {{{ proto int wcl(string filename) */ PHP_FUNCTION(wcl) { char *filename = NULL; int argc = ZEND_NUM_ARGS(); size_t filename_len; char ch; FILE *fp; zend_long lcount = 0; if (zend_parse_parameters(argc, "s", &filename, &filename_len) == FAILURE) return; if ((fp = fopen(filename, "r")) == NULL) { RETURN_FALSE; } while ((ch = fgetc(fp)) != EOF) { if (ch == '\n') { lcount++; } } fclose(fp); RETURN_LONG(lcount); // php_error(E_WARNING, "wcl: not yet implemented"); } /* }}} */ ``` ### 第四步:注冊函數 代碼為hello.c源文件: ```c /* {{{ hello_functions[] * * Every user visible function must have an entry in hello_functions[]. */ const zend_function_entry hello_functions[] = { PHP_FE(confirm_hello_compiled, NULL) /* For testing, remove later. */ PHP_FE(wcl, NULL) PHP_FE_END /* Must be the last line in hello_functions[] */ }; /* }}} */ ``` 通過PHP_FE把函數注冊到zend_function_entry;每個擴展會注冊一個名confirm_xxx_compiled的函數用來輸出當前擴展是否已經被編譯到PHP,通過php -f hello.php,如果出現以下信息表示成功 ```c Functions available in the test extension: confirm_hello_compiled wcl Congratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP. ``` ### 第五步:編譯 ```c cd /opt/php7/php-7.2.10/ext/hello // 去到hello擴展目錄 /opt/php7/php/bin/phpize ./configure --with-php-config=/opt/php7/php/bin/php-config // 指定php-config文件 make sudo make install ``` ### 第六步:php.ini添加擴展 ```c /opt/php7/php/bin/php --ini // 找到配置文件位置 vi /opt/php7/php/ext/php.ini extension=hello.so // 在文件末尾添加 ``` ### 配置項 以上一個基本的擴展已經完成,如果需要設置配置項,如在php.ini注冊一個ini配置項來控制是否計算空行,過程如下: #### php_hello.h聲明擴展內的全局變量 添加代碼如下: ```c ZEND_BEGIN_MODULE_GLOBALS(hello) zend_long filter_blank; ZEND_END_MODULE_GLOBALS(hello) ``` 擴展內全局變量通過HELLO_G(v)讀取,源碼如下: ```c #define HELLO_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(hello, v) ``` #### hello.c添加配置項 ```c ZEND_DECLARE_MODULE_GLOBALS(hello) PHP_INI_BEGIN() STD_PHP_INI_ENTRY("hello.filter_blank", "0", PHP_INI_ALL, OnUpdateLong, filter_blank, zend_hello_globals, hello_globals) PHP_INI_END() ``` 以上代碼為當前擴展注冊了一個配置項hello.filter_blank #### 模塊初始化自動注冊當前擴展配置項 ```c /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(hello) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ REGISTER_INI_ENTRIES(); // 去掉注釋后 return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(hello) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ UNREGISTER_INI_ENTRIES(); // 去掉注釋后 return SUCCESS; } /* }}} */ ``` #### php.ini添加配置 ![](https://box.kancloud.cn/a69dbbca3a4afd1b9f0eed8504a51162_189x59.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看