建立一個新的PHP擴充

來源:互聯網
上載者:User

標籤:php擴充

以此文記錄一下我的第一個PHP擴充。建立PHP擴充步驟:    1.cd /soft/php-7.1.0/ext        進入php源碼目錄,               2. ./ext_skel --extname=hello              產生的副檔名叫hello,會在目前的目錄下建立一個hello的檔案夾           3. cd hello            進入hello檔案夾,檔案夾下最主要的檔案有:                 config.m4 編譯設定檔                php_hello.h  擴充標頭檔                hello.c 擴充源碼            我們需要修改config.m4檔案與hello.c檔案                  4.vi config.m4           修改config.m4檔案,該檔案中,dnl表示注釋,需要將以下3行的dnl去掉並儲存。              PHP_ARG_ENABLE(hello, whether to enable hello support,                 Make sure that the comment is aligned:             [  --enable-hello           Enable hello support])                                      5. vi hello.c                這個檔案是要添加的具體函數,主要需要添加兩處:            1.在confirm_hello_compiled函數下面新添加一個hello函數,這裡這個函數是直接列印字串。                  PHP_FUNCTION(hello)                 {                   php_printf("hello world!\n");                   return;                   }                                  2.需要在 hello_functions數組中加上我們新加的函數              const zend_function_entry hello_functions[] = {                     PHP_FE(confirm_hello_compiled,  NULL)           /* For testing, remove later. */                     PHP_FE(hello,   NULL)           /* 這裡加上新添加的函數名,不要在第一行結尾加 逗號 */                     PHP_FE_END      /* Must be the last line in hello_functions[] */                    };           修改這兩處後儲存並退出。        6. /usr/local/php/bin/phpize              準備編譯我們寫的hello擴充                          7. ./configure --with-php-config=/usr/local/php/bin/php-config             開始編譯                8. make && make install          開始安裝擴充,安裝完成後會顯示擴充安裝的目錄              9. vi php.ini            修改PHP.INI檔案,把hello擴充加上去,如果不確定PHP.INI的位置,可用 php -i |grep php.ini中           找到Loaded Configuration File對應的路徑。如果沒加環境變數則把Php -i 替換成php安裝路徑/bin/php則可。           在; ... or with a path:下新加2行:             extension_dir=/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/            extension=hello.so                      說明 :                extension_dir對應的是擴充目錄,此路徑是在make install成功後顯示的路徑.              extension  是要載入的副檔名稱                        修改完後儲存退出                    10.重啟php-fpm後用 php -m |grep hello 查看擴充是否載入成功        11.建一個測試檔案,測試寫的hello 函數          vi /tmp/test.php             <?php                    hello();                            12.測試hello函數            php -f /tmp/test.php                              OK,大功告成!!!


建立一個新的PHP擴充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.