php擴充套件開發流程

2021-07-04 12:57:18 字數 2680 閱讀 8538

需求:比如開發乙個叫做 heiyeluren 的擴充套件,擴充套件裡就乙個函式 heiyeluren_test(),輸入乙個字串,函式返回:your input string: ***xx。 

要求:了解c/c++程式設計,熟悉php程式設計 

步驟一:生成擴充套件框架

cd /root/soft/php/php-5.2.6/ext

./ext_skel --extname=heiyeluren

cd /root/soft/php/php-5.2.6/ext/heiyeluren

vi config.m4

開啟檔案後去掉 dnl ,獲得下面的資訊:

php_arg_enable(heiyeluren, whether to enable heiyeluren support,

[  --enable-heiyeluren           enable heiyeluren support])

儲存退出.

(圖01)

第二步:編寫**

vi php_heiyeluren.h

找到:php_function(confirm_heiyeluren_compiled); ,新增一行:

php_function(heiyeluren_test);

儲存退出。

(圖02)

vi heiyeluren.c

陣列裡增加我們的函式,找到 zend_function_entry heiyeluren_functions,增加:

php_fe(heiyeluren, null)

(圖03)

再到 heiyeluren.c 檔案最後面增加如下**:

php_function(heiyeluren_test)

len = spprintf(&strg, 0, "your input string: %s\n", arg);

return_stringl(strg, len, 0);

}儲存退出。

(圖04)

第三步:編譯安裝

cd /root/soft/php/php-5.2.6/ext/heiyeluren

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make test

make install

現在看看是不是有個 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/heiyeluren.so

編輯php.ini,把擴充套件加入進去:

vi /usr/local/php/lib/php.ini

在[php]模組下增加:

extension = heiyeluren.so

儲存退出。

(圖05)

注意:如果你不存在擴充套件檔案目錄,或者安裝報錯,那麼可以自行建立這個目錄,然後把擴充套件拷貝到目錄下,然後記得把 php.ini 檔案中的 extension_dir 修改為該目錄:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

(圖06)

第四步:檢查安裝結果

現在看看模組載入了沒有:

/usr/local/php/bin/php -m,應該會列印出:

[php modules]

...heiyeluren

...[zend modules]

然後重啟apache,輸出 phpinfo() ,應該能夠看到:

heiyeluren

heiyeluren support enabled

(圖07)

看看函式是否存在並且呼叫,在web目錄下建立:heiyeluren.php

<?php

echo "

";

print_r(get_loaded_extensions());

print_r(get_extension_funcs('heiyeluren'));

echo heiyeluren_test('my first php extension');

echo "

";

?>

訪問apache,應該能夠看到:

array

(...

[33] => heiyeluren

)array

([0] => confirm_heiyeluren_compiled

[1] => heiyeluren_test

)your input string: heiyeluren

(圖08)

php擴充套件開發

php擴充套件 pecl 跟php引擎一樣都是使用c語言開發。php核心開發組成員鳥哥laruence使用的是vim進行php開發。書籍 案例 php src ext pecl開發郵件組 盡量編寫一些phpt測試用例,php src tests下有很多參考.測試時用 enable debug編譯ph...

php擴充套件開發

二 php擴充套件開發 擴充套件開發流程 生成開發骨架 修改config.m4 編碼 編譯擴充套件為so 修改php.ini 1.生成開發骨架 1.1 進入擴充套件目錄 cd php 7.0.1 ext 1.2 用.ext skel生成骨架 ext skel extname module 1.3 修...

PHP擴充套件開發

php function say hello len spprintf strg,0,hello s n arg return stringl strg,len,0 1.引數接收 這裡接收函式的引數需要通過zend parse parameter函式解析,第乙個引數指定使用者傳入say hello函...