PHP扩展开发 - 构建第一个PHP扩展
2019獨角獸企業重金招聘Python工程師標準>>>
首先需要確定系統中安裝了gcc編譯器,合適版本的bison等
####構建一個基本的擴展骨架 在PHP擴展開發時,使用ext_skel完成擴展的結構骨架創建。
$ ./ext_skel ./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]][--skel=dir] [--full-xml] [--no-help]--extname=module 這里的module是要創建的擴展名稱--proto=file 這里的file文件包含了要創建的函數的原型--stubs=file generate only function stubs in file--xml generate xml documentation to be added to phpdoc-cvs--skel=dir 創建擴展骨架的目錄--full-xml generate xml documentation for a self-contained extension (not yet implemented)--no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled注意: ext_skel命令文件在源文件的ext目錄下。
這里的--extname參數是要創建的擴展名稱,擴展名稱為 小寫字母 + 下劃線 組成,并且, 在ext目錄中必須是唯一的。
例如,這里要創建一個名為ext_demo_1的PHP擴展:
/vagrant/ext$ ./ext_skel --extname=ext_demo_1 Creating directory ext_demo_1 Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done].To use your new extension, you will have to execute the following steps:1. $ cd .. 2. $ vi ext/ext_demo_1/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-ext_demo_1 5. $ make 6. $ ./php -f ext/ext_demo_1/ext_demo_1.php 7. $ vi ext/ext_demo_1/ext_demo_1.c 8. $ makeRepeat steps 3-6 until you are satisfied with ext/ext_demo_1/config.m4 and step 6 confirms that your module is compiled into PHP. Then, start writing code and repeat the last two steps as often as necessary.現在,在ext目錄下出現了一個新建的擴展目錄ext_demo_1:
/vagrant/ext/ext_demo_1$ ls config.m4 CREDITS ext_demo_1.c php_ext_demo_1.h config.w32 EXPERIMENTAL ext_demo_1.php tests這時,該擴展是無法編譯通過的,需要先編輯config.m4文件才行。
####配置文件config.m4
配置文件config.m4告訴UNIX構建系統擴展支持的configure選項以及擴展需要的額外的庫, 包含哪些源文件等,該文件使用的是GNU的autoconf語法,以dnl開頭的行為注釋,使用中括號([和])包含的為字符串。
autoconf語法參見 AUTOCONF文檔
PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support, [ --enable-ext_demo_1 Enable ext_demo_1 support])if test "$PHP_EXT_DEMO_1" != "no"; thenPHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared) fi上述為autoconf的配置文件,第一個宏PHP_ARG_ENABLE,含有三個參數:
- ext_demo_1 這是第一個參數,為./configure建立了名為enable-ext_demo_1的選項
- 第二個參數將會在./configure命令處理到該擴展的配置文件時,顯示該參數的內容
- 第三個參數是./configure命令的幫助,在使用./configure --help的時候顯示
第二個宏為PHP_NEW_EXTENSION,該宏聲明了擴展的模塊和必須要編譯作為擴展一部分的源碼文件。 如果需要多個源文件,則使用空格分隔,第三個參數$ext_shared與調用 PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)有關。
PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)####編譯擴展
修改完config.m4文件之后,接下來編譯PHP和擴展。
/vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php /vagrant$ make /vagrant$ sudo make install Installing PHP SAPI module: cgi Installing PHP CGI binary: /usr/local/php/bin/ Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/man/man1/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/program: phpizeprogram: php-config Installing man pages: /usr/local/php/man/man1/page: phpize.1page: php-config.1 /vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/此時,PHP安裝在了/usr/local/php目錄下,進入該目錄,可以看到如下文件:
/usr/local/php$ ls bin include lib man進入/usr/local/php/bin目錄,執行以下命令:
/usr/local/php/bin$ ./php --info|grep demo Configure Command => './configure' '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php' ext_demo_1 ext_demo_1 support => enabled可以看到,phpinfo()中擴展支持已經啟用了,按照上述步驟安裝的擴展中包含了一個測試擴展是否能夠正常工作的函數, 該函數名為confirm_ext_demo_1_compiled(arg),執行結果如下:
/usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');" Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.可以看到,ext_demo_1擴展安裝成功了。
我的博客:http://aicode.cc/,在這里可以看到更多相關文章。
轉載于:https://my.oschina.net/agiledev/blog/343162
總結
以上是生活随笔為你收集整理的PHP扩展开发 - 构建第一个PHP扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#编码、解码
- 下一篇: 动态规划算法php,php算法学习之动态