PHP7擴充套件開發之hello word

2021-09-23 17:49:51 字數 2287 閱讀 9881

本文是以php7作為基礎,講解如何從零開始建立乙個php擴充套件。本文主要講解建立乙個擴充套件的基本步驟都有哪些。示例中,我們將實現如下功能:

<?php

echo say();

?>

輸出內容:

$ php ./test.php

$ hello word

在擴充套件中實現乙個say方法,呼叫say方法後,輸出 hello word。

php為我們提供了生成基本**的工具 ext_skel。這個工具在php源**的./ext目錄下。

$ cd php_src/ext/

$ ./ext_skel --extname=say

extname引數的值就是擴充套件名稱。執行ext_skel命令後,這樣在當前目錄下會生成乙個與副檔名一樣的目錄。

config.m4的作用就是配合phpize工具生成configure檔案。configure檔案是用於環境檢測的。檢測擴充套件編譯執行所需的環境是否滿足。現在我們開始修改config.m4檔案。

$ cd ./say

$ vim ./config.m4

開啟,config.m4檔案後,你會發現這樣一段文字。

dnl if your extension references something external, use with:

dnl php_arg_with(say, for say support,

dnl make sure that the comment is aligned:

dnl [ --with-say include say support])

dnl otherwise use enable:

dnl php_arg_enable(say, whether to enable say support,

dnl make sure that the comment is aligned:

dnl [ --enable-say enable say support])

其中,dnl 是注釋符號。上面的**說,如果你所編寫的擴充套件如果依賴其它的擴充套件或者lib庫,需要去掉php_arg_with相關**的注釋。否則,去掉 php_arg_enable 相關**段的注釋。我們編寫的擴充套件不需要依賴其他的擴充套件和lib庫。因此,我們去掉php_arg_enable前面的注釋。去掉注釋後的**如下:

dnl if your extension references something external, use with:

dnl php_arg_with(say, for say support,

dnl make sure that the comment is aligned:

dnl [ --with-say include say support])

dnl otherwise use enable:

php_arg_enable(say, whether to enable say support,

make sure that the comment is aligned:

[ --enable-say enable say support])

修改say.c檔案。實現say方法。

找到php_function(confirm_say_compiled),在其上面增加如下**:

php_function(say)

找到 php_fe(confirm_say_compiled, 在上面增加如下**:

php_fe(say, null)
修改後的**如下:

const zend_function_entry say_functions = ;

/* }}} */

編譯擴充套件的步驟如下:

$ phpize

$ ./configure

$ make && make install

修改php.ini檔案,增加如下**:

[say]

extension = say.so

然後執行,php -m 命令。在輸出的內容中,你會看到say字樣。

第五步,呼叫測試

自己寫乙個指令碼,呼叫say方法。看輸出的內容是否符合預期。

PHP7擴充套件開發之hello word

原文出處 本文是以php7作為基礎,講解如何從零開始建立乙個php擴充套件。本文主要講解建立乙個擴充套件的基本步驟都有哪些。示例中,我們將實現如下功能 echo say 輸出內容 php test.php hello word 在擴充套件中實現乙個say方法,呼叫say方法後,輸出 hello wo...

PHP7擴充套件開發之hello word

echo say 輸出內容 php test.php hello word在擴充套件中實現乙個say方法,呼叫say方法後,輸出 hello word。php為我們提供了生成基本 的工具 ext skel。這個工具在php源 的.ext目錄下。cd php src ext ext skel extn...

PHP7擴充套件開發之hello word

本文是以php7作為基礎,講解如何從零開始建立乙個php擴充套件。本文主要講解建立乙個擴充套件的基本步驟都有哪些。示例中,我們將實現如下功能 echo say 輸出內容 php test.php hello word 在擴充套件中實現乙個say方法,呼叫say方法後,輸出 hello word。ph...