首頁 php教程 php手册 Hello World: 第一个PHP扩展

Hello World: 第一个PHP扩展

Jun 13, 2016 am 10:45 AM
php world 創建 唯一 實現 擴充 目標 第一個

 

目标
创建一个名为 hello 的 PHP 扩展,并实现里面唯一的函数 hello_world,作用是打印出 "Hello World" 字符串。

前提条件
一台已经安装了 C 编译器、PHP 运行环境的电脑,一个称手的文本编辑器。
重要提示:不要试图在 Windows 下写 PHP 扩展,Visual C、MinGW 的编译器都不好用,我曾经捣鼓了一个多星期也没能在 Windows 下编译成功过。所以至少要在一个 Unix 环境下进行。Mac 和各种 Linux 环境都可以。

下载 PHP 源代码
先用 php -v 确定系统上的 PHP 版本,再到 php.net 上下载相应的源代码包。解压到某个目录下,如 php5-5.3.5。源代码目录里,ext 目录下即是所有的 PHP 扩展所在的地方,其他的目录暂时不必考虑。

生成 PHP 扩展的框架代码
在 php5-5.3.5/ext 目录下,有一个名为 ext_skel 的文件,这是用来创建扩展的一个简便的工具。确保它有可执行权限(chmod u+x ext_skel),在终端下执行

./ext_skel --extname=hello
登入後複製

即会在 ext 目录下创建一个 hello 的目录,里面是初始的骨架代码。下一步的任务是创建 hello 扩展,并实现 hello_world 函数。

编辑 config.m4
用文本编辑器打开 ext/hello/config.m4,里面有大量的注释说明(以 dnl 开头的行),基本上已经把很多问题说明白了。这里要做的就是把

dnl PHP_ARG_ENABLE(hello, whether to enable hello support,
dnl Make sure that the comment is aligned:
dnl [  --enable-hello           Enable hello support])
登入後複製

这三行取消注释。这样在接下来的编译时,可以用 ./configure --enable-hello 来编译我们刚刚写的扩展。

重新生成 configure
回到源代码根目录,运行 ./buildconf --force,会激活 configure --enable-hello 参数。如果你在运行 buildconf 时报下面的错误:

buildconf: Your version of autoconf likely contains buggy cache code.
           Running vcsclean for you.
           To avoid this, install autoconf-2.13.
登入後複製

请安装 autoconf-2.13(ubuntu 懒人的用法)

sudo apt-get install autoconf2.13
登入後複製

编译扩展
此时的 hello 扩展已经可以编译了,虽然还没有实现其中的 hello_world 函数。先编译一下,确保没有环境配置上的问题。

./configure --enable-hello
make
登入後複製

经过一段时间(其实是把整个 PHP 也编译出来了),用

./sapi/cli/php -f ext/hello/hello.php
登入後複製

检查一下扩展编译情况。不出意外的话,应该提示

Functions available in the test extension:
confirm_hello_compiled

Congratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP.
登入後複製

编写 hello_world 函数
声明函数:打开 ext/hello/php_hello.h,在

		PHP_MINIT_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
PHP_MSHUTDOWN_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
PHP_RINIT_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
PHP_RSHUTDOWN_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
PHP_MINFO_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
登入後複製

后面添加

		PHP_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello_world<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
登入後複製

即在扩展的头文件中声明了 hello_world 的函数的原型。PHP_FUNCTION 是用来定义 PHP 函数的 C 语言宏。至于宏展开后的样子,几乎不用去想。只管用就可以了。

实现函数:打开 hello.c,在文件的末尾添加

		PHP_FUNCTION<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello_world<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">{</span>
	php_printf<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(255, 0, 0); ">"Hello World"</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(177, 177, 0); ">return</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">}</span>
登入後複製

这里即是 hello_world 函数的实现。php_printf 的作用是向 SAPI 输出一段字符串,类似于 PHP 语言中的 echo。

接下来还需要将 hello_world 函数注册到 zend_module_entry,这样这个函数才能在 PHP 程序中变成“可见”的。找到

		<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(153, 51, 51); ">const</span> zend_function_entry hello_functions<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">[</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">]</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">=</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">{</span>
	PHP_FE<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>confirm_hello_compiled<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span>	NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; ">/* For testing, remove later. */</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">{</span>NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span> NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span> NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">}</span><span style="font-family: Tahoma, sans-serif; line-height: normal; ">/* Must be the last line in hello_functions[] */</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">}</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
登入後複製

将其修改为:

		<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(153, 51, 51); ">const</span> zend_function_entry hello_functions<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">[</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">]</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">=</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">{</span>
	PHP_FE<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>confirm_hello_compiled<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span>	NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; ">/* For testing, remove later. */</span>
	PHP_FE<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">(</span>hello_world<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span> NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">)</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">{</span>NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span> NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">,</span> NULL<span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">}</span><span style="font-family: Tahoma, sans-serif; line-height: normal; ">/* Must be the last line in hello_functions[] */</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(0, 153, 0); ">}</span><span style="font-family: Tahoma, sans-serif; line-height: normal; color: rgb(51, 153, 51); ">;</span>
登入後複製

此时整个的 hello 扩展的代码就编写完了。最后再来 make 一下。

测试
在终端下运行 sapi/cli/php -r 'hello_world();echo "\n";',如果看到输出“Hello World”,就成功了。

如何把扩展编译成 .so 文件
上面编译的结果是把 hello 扩展编译进了 PHP 核心中。如果想要编译成 .so 扩展,以便发布出去的话。需要使用

./configure --enable-hello=shared
make
登入後複製

这样编译完成后,会在 modules 目录下生成 hello.so 文件。把它复制到你的 PHP 运行环境的 extension_dir 下就可以像其他扩展一样使用了。需要注意的是 PHP 版本。如果你是在 PHP 5.3.5 的源代码环境中编译的扩展,则生成的 .so 文件只能用在 PHP 5.3.5 的运行环境中。

最后提一下,如果对 PHP 扩展有兴趣,可以看看《Extending and Embedding PHP》这本书,作者还是个 MM。目前没有中文版,英文电子版的自己搜。

摘自 lostwolf blog

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles