PHP C拓展開發入門筆記(一)

2021-08-21 12:44:32 字數 1496 閱讀 2933

首先使用php拓展構建ext_skel工具構建出拓展框架

cd /path/to/php-src/ext

# 生成名為myext的c拓展框架

./ext_skel --extname=myext

#進入拓展原始碼目錄

cd myext

# |-config.m4 unix build system configuration

# |-config.w32 windows buildsystem configuration

# |-

# |-

#修改config.m4,config.m4是autoconf工具的配置檔案,用來修改各種編譯選項。

vi config.m4

#dnl php_arg_enable(myext, whether to enable myext support,

#dnl make sure that the comment is aligned:

#dnl [ --enable-myext enable myext support])

#改為#php_arg_enable(myext, whether to enable myext support,

#dnl make sure that the comment is aligned:

#[ --enable-myext enable myext support])

#依次執行

phpize #used to prepare the build environment for a php extension

./configure

make

make install

#即可完成拓展包動態鏈結庫的生成和安裝,然後在php.ini 中加入開啟拓展即可

vi php.ini

#加入 extension=myext.so

#重啟apache或php-fpm

以上構建了乙個預設的空拓展,下面加入一些自己寫的函式並呼叫

//定義my_sum()函式,傳遞兩個引數計算和並列印字串

php_function(my_sum)

len = spprintf(&strg, 0, "%d+%d=%d\n",i,j,i+j);

php_printf("%s\n",strg);

}//下面函式會在查詢phpinfo時顯示

php_minfo_function(myext)

//建立zend_function_entry陣列,提供給zend作為php的介面。

const zend_function_entry myext_functions =

//儲存重新make && make install並重啟

#測試

php -r "my_sum(111,234);"

官方文件在這裡:

PHP 拓展 開發,PHP擴充套件開發 第乙個擴充套件

我們先假設業務場景,是需要有這麼乙個擴充套件,提供乙個叫ccvita string的函式,他的主要作用是返回一段字元。這個業務場景實在太假,大家就這麼看看吧 對應的php 可能是這樣 function ccvita string str result link return result 第一步,生...

PHP擴充套件開發入門

寫乙個最簡單的將字串全部變成大寫的函式 function my toupper str echo my toupper demo 現在我們開發乙個php擴充套件,實現my toupper的功能。step1 php提供了乙個擴充套件框架生成器 ext skel,這個工具在php原始碼的ext目錄 我的...

PHP7擴充套件開發入門

我們可以在ext目錄下看到所有的php原生擴充套件,其中包括了熟悉的curl,json,mbstring,xml,sockets等擴充套件,還有很多沒有用過甚至沒有聽說過的擴充套件,不用在意這些,我們先開啟我們最熟悉的curl來看看,有config.m4配置檔案,有php curl.h,curl f...