通常在開發PHP的時候,一些核心代碼,比如加密函數或需要高效率執行的代碼,此時可以用C語言寫擴展。本文主要介紹了擴展的開發流程,具體的代碼實現參考生成的文件說明。
當前PHP使用的是XAMPP 5.6.8,安裝路徑是:/Applications/XAMPP/。
1. 開始之前,從網站上下載php 5.6.8的擴展,下載地址:http://php.net/get/php-5.6.8.tar.bz2/from/a/mirror
2. 解壓后進入ext目錄,執行:./ext_skel --extname=test,會在ext目錄下生成test目錄,此目錄下建立了擴展名稱為test的開發框架
3. 進入test目錄,編輯config.m4文件
將如下行的注釋標簽"dnl"去掉,修改后如下所示:
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[ --enable-myfunctions Enable myfunctions support])
4. 使用phpize生成configure文件(phpize路徑為:/Applications/XAMPP/xamppfiles/bin/phpize)
5. 執行命令:./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config
6. 執行命令: make編譯擴展
7. 執行命令:sudo make install 安裝擴展
8. 修改php.ini文件(路徑:/Applications/XAMPP/xamppfiles/etc/php.ini)
9. 重啟apache,依次執行一下命令:
sudo /Applications/XAMPP/xamppfiles/apache2/scripts/ctl.sh stop
sudo /Applications/XAMPP/xamppfiles/apache2/scripts/ctl.sh start