Hello World: 第一个PHP扩展
目标
创建一个名为 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

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP는 오픈 소스 MVC 프레임워크입니다. 이를 통해 애플리케이션 개발, 배포 및 유지 관리가 훨씬 쉬워집니다. CakePHP에는 가장 일반적인 작업의 과부하를 줄이기 위한 여러 라이브러리가 있습니다.

이 튜토리얼은 PHP를 사용하여 XML 문서를 효율적으로 처리하는 방법을 보여줍니다. XML (Extensible Markup Language)은 인간의 가독성과 기계 구문 분석을 위해 설계된 다목적 텍스트 기반 마크 업 언어입니다. 일반적으로 데이터 저장 AN에 사용됩니다
