用C開發PHP擴充套件 例項(基礎版)

2021-12-30 02:32:43 字數 2259 閱讀 7499

第一步:建立擴充套件骨架。

[html]

cd /usr/local/src/php-5.3.6/ext/ 

./ext_skel --extname=laiwenhui 

cd /usr/local/src/php-5.3.6/ext/

./ext_skel --extname=laiwenhui第二步:修改編譯引數。

[html] view plaincopyprint?cd php-5.3.6/ext/laiwenhui 

vi config.m4 

cd php-5.3.6/ext/laiwenhui

vi config.m4去掉

php_arg_enable(laiwenhui, whether to enable laiwenhui support,

[  --enable-laiwenhui           enable laiwenhui support])

兩行前面的dnl

修改後為:

dnl otherwise use enable:

php_arg_enable(laiwenhui, whether to enable laiwenhui support,

dnl make sure that the comment is aligned:

[  --enable-laiwenhui           enable laiwenhui support])

第三步:編寫**

[html]

vim  php_laiwenhui.h 

vim  php_laiwenhui.h

在php_function(confirm_laiwenhui_compiled);

後面新增一行

php_function(test);

[html]

vim laiwenhui.c 

vim laiwenhui.c

在php_fe(confirm_laiwenhui_compiled, null)

後面新增

php_fe(test, null)

**在檔案最後面增加如下**:**

[html]

php_function(test) 

php_function(test)

第四步:編譯**

[html]

/usr/local/php/bin/phpize 

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

make 

make install 

/usr/local/php/bin/phpize

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

make

make install

我的php安裝路徑為:/usr/local/php

這個時候會生成乙個檔案 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui.so

編輯php配置檔案php.ini,新增擴充套件:

[html]

vim php.ini 

vim php.ini在[php]模組下增加:

[html]

extension = laiwenhui.so 

extension = laiwenhui.so把php.ini 檔案中的 extension_dir 修改為該目錄:

[html]

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

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

第五步:檢查安裝結果

1.重啟php-fpm,ngixn

2./use/local/php/bin/php -m

看下是否有包含laiwenhui擴充套件。

第六步:執行測試**

在**根目錄建立test.php

vim test.php

**內容如下

<?php

echo test();

執行後結果為:

this is my first extention!

如果你能順利完成以上幾步,恭喜你完成了第乙個擴充套件。

用C開發PHP擴充套件 例項(基礎版)

cd usr local src php 5.3.6 ext ext skel extname laiwenhuicd php 5.3.6 ext laiwenhui vi config.m4去掉 php arg enable laiwenhui,whether to enable laiwenhu...

用C開發PHP擴充套件初探

函式功能 php裡面的整數是有符號數,其內部實現其實就是long,不是unsigned long。對於32位機器來說,php最大能表示的整數就是2 31 1了,一般在應用中碰到大於2 31 1而小於2 32的數就只能用字串來表示了。對於mixed int ext string in 來說,如果字串i...

php擴充套件類開發例項

1 class vector2d214 15 16 generates a copy of this vector.17 return vector2d a copy of this vector.18 19public function mycopy 2023 24 25 sets this ve...