php-extension - php擴充開發的問題。

來源:互聯網
上載者:User
現在網上有很多PHP開發的的教程和關於PHP底層的WIKI,我想如果想開發PHP7的擴充看這些有用嗎?還是這些只能開發PHP5幾的擴充?

回複內容:

現在網上有很多PHP開發的的教程和關於PHP底層的WIKI,我想如果想開發PHP7的擴充看這些有用嗎?還是這些只能開發PHP5幾的擴充?

思路差不多的.PHP7原始碼ext下的擴充的代碼可以說都是標準範例.
還有,鳥哥的Yaf/Yar/Yac等等,峰哥的Swoole都已經支援PHP7了,
這些第三方擴充可以在 pecl.php.net 上找到.

http://www.laruence.com/2011/09/13/2139.html
入門: http://www.walu.cc/phpbook/
案例: php-src/ext
PECL開發郵件組: http://news.php.net/php.pecl.dev
盡量編寫一些phpt測試案例,php-src/tests下有很多參考.
測試時用--enable-debug編譯PHP,要做到執行你的擴充邏輯,不輸出任何錯誤資訊.
用valgrind檢測記憶體泄露.

Swoole作者峰哥教你怎麼構建PHP擴充(視頻教程):
http://wiki.swoole.com/wiki/page/238.html
http://php.net/manual/zh/internals2.buildsys.php
php-src/ext/ext_skel指令碼用於產生PECL擴充源碼骨架.

PECL擴充開發步驟(假設你的副檔名叫codenc):
cd php-src/ext
./ext_skel --extname=codenc
Creating directory codenc
Creating basic files:
config.m4 擴充的configure設定檔(Linux)
config.w32 擴充的configure設定檔(Windows)
codenc.c 擴充主檔案
php_codenc.h 擴充標頭檔(定義擴充版本號碼PHP_CODENC_VERSION)
codenc.php 顯示擴充提供的函數
tests/001.phpt 測試指令碼
CREDITS 鳴謝
EXPERIMENTAL 實驗
.svnignore SVN忽略目錄或檔案.deps,.lo,.la

[done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/codenc/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-codenc5.  $ make6.  $ ./sapi/cli/php -f ext/codenc/codenc.php7.  $ vi ext/codenc/codenc.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/codenc/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

編輯config.m4:
把:

dnl PHP_ARG_ENABLE(codenc, whether to enable codenc support,dnl Make sure that the comment is aligned:dnl [  --enable-codenc           Enable codenc support])

改成:

PHP_ARG_ENABLE(codenc, whether to enable codenc support,[  --enable-codenc           Enable codenc support])

其中dnl是注釋符號.
編輯config.w32:
把:

// ARG_ENABLE("codenc", "enable codenc support", "no");

改為:

ARG_ENABLE("codenc", "enable codenc support", "no");

執行phpize根據config.m4產生擴充的configure指令碼:

/png/php/5.4.39NTS/bin/phpizeConfiguring for:PHP Api Version:         20100412Zend Module Api No:      20100525Zend Extension Api No:   220100525

執行configure產生Makefile用於make編譯:

./configure --with-php-config=/png/php/5.4.39NTS/bin/php-configchecking ...config.status: creating config.h

執行 make && make install 編譯並安裝擴充:

Libraries have been installed in: /home/eechen/png_stack/php-5.4.39/ext/codenc/modulesInstalling shared extensions: /png/php/5.4.39NTS/lib/php/extensions/no-debug-non-zts-20100525/

在 php.ini 載入擴充:

extension=codenc.so

測試:

php -r 'echo confirm_codenc_compiled("codenc")."\n";'

輸出:
Congratulations! You have successfully modified ext/codenc/config.m4.
Module codenc is now compiled into PHP.
開啟 php_codenc.h 和 codenc.c 可見其自動產生了一個用於測試的函數 confirm_codenc_compiled

php_codenc.h(聲明):PHP_FUNCTION(confirm_codenc_compiled);    /* For testing, remove later. */codenc.c(註冊跟實現):const zend_function_entry codenc_functions[] = {    PHP_FE(confirm_codenc_compiled,    NULL)        /* For testing, remove later. */    PHP_FE_END    /* Must be the last line in codenc_functions[] */};PHP_FUNCTION(confirm_codenc_compiled) {}

修改代碼後,重新執行 make && make install 編譯安裝.

PECL模組載入卸載時執行的函數:
MINIT: Module Init (Beast和Blenc模組解密時會在這裡重寫zend_compile_file.緩衝模組APC和Opcache也是在此起作用)
RINIT: Request Init (Session模組和VLD擴充都在這裡起作用,VLD能夠相容Beast和Opcache)
RSHUTDOWN: Request Shutdown (register_shutdown_function,FPM提供的fastcgi_finish_request在這裡起作用)
MSHUTDOWN: Module Shutdown (Beast和Blenc模組在這裡重設函數 zend_compile_file = old_compile_file)

PHP_MINIT_FUNCTION(MyModule);
當模組被Zend Engine載入後,例如Apache或者PHP-FPM啟動,載入了PHP模組,
Zend Engine會對每一個擴充模組調用此函數(如果有的話),可以在該函數裡進行一些初始化操作.

PHP_MSHUTDOWN_FUNCTION(MyModule);
當Zend Engine收到shutdown訊號後,例如Apache卸載PHP模組,
Zend Engine對每一個模組調用此函數,最後關閉自己的核心子系統.

PHP_RINIT_FUNCTION(MyModule);
對於每一個使用該模組的PHP指令碼請求前,都執行該函數(如果有的話).
最好的例子:Session擴充模組,如果在一個PHP指令碼裡執行session.start(),
Session模組的PHP_RINIT_FUNCTION()將被調用.

PHP_RSHUTDOWN_FUNCTION(MyModule);
與PHP_RINIT_FUNCTION()相反,該函數是在一個PHP指令碼執行完畢後執行,
比如PHP-FPM提供的函數fastcgi_finish_request.

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.