PHP扩展开发(1):入门,php扩展
PHP扩展开发(1):入门,php扩展
有关PHP扩展开发的文章、博客已经很多了,比较经典的有:
我准备在此系列博文中总结我有关PHP扩展开发的学习和感悟,力图简单清晰地描述在Linux系统下开发一个PHP扩展应该具备的最基本知识。水平较低,难免有错误,望指出。
准备工作
首先要获取一份PHP源码(可以从Github上签出,或者到官网上下载最新的稳定版),然后编译之。为了加快编译速度,我们推荐禁用所有额外的扩展(使用--disable-all选项),但最好打开debug(使用--enable-debug选项)和线程安全(使用--enable-maintainer-zts),但要在发布扩展的时候关闭debug,视情况选择是否需要打开线程安全:
$ ./buildconf --<span>force $ .</span>/configure --disable-all --enable-debug --enable-maintainer-<span>zts $ </span><span>make</span>
注意,我们没有指定--prefix选项(同时也没有make install),因为这不是必须的。注意查看输出信息,也许你需要安装一些依赖包才能成功编译PHP。
编译后的PHP的可执行程序在源码的sapi目录下,对应不同的宿主环境有不同的子目录,我们以后都主要使用cli(command line interface)环境,可以建一个别名方便引用:
$ alias php-dev=/usr/local/src/php-<span>5.6</span>.<span>5</span>/sapi/cli/php
有一些命令行选项是很有用的:
php-dev -<span>h # 打印帮助信息 php</span>-dev -<span>v # 打印版本信息 php</span>-dev --<span>ini # 打印配置信息 php</span>-dev -<span>m # 打印加载的模块信息 php</span>-dev -<span>i # phpinfo php</span>-dev -r <code> # 执行code里的代码
扩展骨架
PHP的所有官方扩展都在源码的ext目录下,我们自己写的扩展也可以放在该目录下。注意,该目录下有个名为ext_skel的shell脚本,它是用来生成PHP扩展骨架的,使用该脚本,可以帮我们快速创建PHP扩展:
$ ./ext_skel --extname=myext
上面的命令帮我们创建了一个名为myext的扩展,源码在myext目录下。不带任何参数的执行该脚本可以打印帮助信息,这样你可以查看到该脚本提供的更多选项。
接下来让我们完成我们的扩展。进入myext目录,编辑config.m4配置文件,找到PHP_ARG_ENABLE宏函数,去掉前面的dnl注释(共三行)。退回到源码根目录,重新执行buildconf、configure和make命令:
$ ./buildconf --<span>force $ .</span>/configure --help | <span>grep</span><span> myext </span>--enable-<span>myext Enable myext support $ .</span>/configure --disable-all --enable-myext --enable-debug --enable-maintainer-<span>zts $ </span><span>make</span>
注意,我们用./configure --help | grep myext打印了我们扩展的加载情况,如果看不到下面的输出,则说明我们的扩展没有配置成功,回头检查下config.m4文件。
这次编译应该非常快,因为大部分代码都已经编译过了。PHP还有另外一种编译扩展的方法(使用动态连接的方式,将扩展编译为.so的文件),不过我们推荐在开发扩展的时候使用静态编译,因为这样省去了在配置文件中加载扩展的步骤。
一切顺利的话,我们的第一个扩展就已经可以执行了:
$ php-dev -m | <span>grep</span><span> myext myext $ php</span>-dev -r <span>'</span><span>echo confirm_myext_compiled("myext") . "\n";</span><span>'</span><span> Congratulations</span>! You have successfully modified ext/myext/config.m4. Module myext is now compiled into PHP.
第一个命令显示了我们的扩展已经被加载。第二个命令执行了ext_skel扩展骨架自动为我们创建的函数。当然,这个函数毫无意义,不过我们可以很容易的把这个函数改编成hello world。
手动创建扩展
大部分教程都是以ext_skel扩展骨架为原型讲述扩展开发的,这种做法当然很方便快捷。但是我个人更喜欢纯手工开发扩展的方式,因为这样更容易理解其中的每一个细节。
手动创建扩展,先进入ext目录,创建我们的扩展目录myext2。有几个文件是必须的:config.m4,myext2.c和php_myext2.h。
首先,我们来编写配置文件config.m4:
PHP_ARG_ENABLE(myext2, whether to enable myext2 support, <span>[</span><span> --enable-myext2 Enable myext2 support</span><span>]</span><span>) </span> if test "PHP_MYEXT2" != "no"<span>;</span><span> then</span> PHP_NEW_EXTENSION(myext2, myext2.c,<span> $ext_shared) </span>fi
config.m4其实是autoconf程序使用的配置文件,autoconf是autotools工具箱里重要的组成。完整介绍autoconf的用法是需要很长时间的,好在我们这里的用法非常简单。
PHP_ARG_ENABLE是PHP为autoconf定义的宏函数,myext2是它的第一个参数,指出了扩展的名字;后面两个参数只是在make和configure执行时用来显示的,所以我们可以随便写。[ ]在autoconf语法中的作用类似于双引号,用来包裹字符串(注意第二个参数中包含了空格,但是可以不用方括号起来)。还有第四个参数用来指明扩展默认是开启还是关闭(yes或no),默认是no。
下面三行其实就是shell语法,判断我们是否开启了PHP_MYEXT2扩展模块。如果开启了该扩展模块(--enable-myext2),则$PHP_MYEXT2变量的值不为no,因此执行PHP_NEW_EXTENSION宏。这个宏函数也是PHP为autoconf定义的扩展语法,第一个参数同样是扩展名称;第二个参数是扩展要编译的C文件,如果有多个,依次写下去就可以了(空格分隔);第三个参数固定是$ext_shared。
接下来编写php_myext2.h头文件,该文件的命名是PHP扩展的规范 — php_扩展名.h:
<span> 1</span> <span>#ifndef PHP_MYEXT2_H </span><span> 2</span> <span>#define</span> PHP_MYEXT2_H <span> 3</span> <span> 4</span> <span>extern</span><span> zend_module_entry myext2_module_entry; </span><span> 5</span> <span>#define</span> phpext_myext2_ptr &myext2_module_entry <span> 6</span> <span> 7</span> <span>#define</span> PHP_MYEXT2_VERSION "0.1.0" <span> 8</span> <span> 9</span> <span>/*</span><span> prototypes </span><span>*/</span> <span>10</span> <span>PHP_FUNCTION(hello); </span><span>11</span> <span>12</span> <span>#endif</span> /* PHP_MYEXT2_H */
这里主要的代码是定义了名为phpext_myext2_ptr的宏,PHP底层通过该宏来引用我们的扩展。可以看出,该宏的命名同样是有规范的 — phpext_扩展名_ptr。而myext2_module_entry是我们稍后要在.c文件里定义的结构体,它的命名也是规范的 — 扩展名_module_entry。
此外我们还定义了一个标识我们扩展版本号的宏和一个函数原型(通过PHP_FUNCTION宏,PHP_FUNCTION宏函数的参数是外部可使用的函数名),稍后我们会来实现这个函数。
最后来看下myext2.c文件的实现:
<span> 1</span> #include <span>"</span><span>php.h</span><span>"</span> <span> 2</span> #include <span>"</span><span>php_myext2.h</span><span>"</span> <span> 3</span> <span> 4</span> <span>/*</span><span> {{{ myext2_functions[] </span><span> 5</span> <span> * </span><span> 6</span> <span> * Every user visible function must have an entry in myext2_functions[]. </span><span> 7</span> <span>*/</span> <span> 8</span> <span>static</span> <span>const</span> zend_function_entry myext2_functions[] =<span> { </span><span> 9</span> <span> PHP_FE(hello, NULL) </span><span>10</span> <span> PHP_FE_END </span><span>11</span> <span>}; </span><span>12</span> <span>/*</span><span> }}} </span><span>*/</span> <span>13</span> <span>14</span> <span>/*</span><span> {{{ myext2_module_entry </span><span>15</span> <span>*/</span> <span>16</span> zend_module_entry myext2_module_entry =<span> { </span><span>17</span> <span> STANDARD_MODULE_HEADER, </span><span>18</span> <span>"</span><span>myext2</span><span>"</span>, <span>/*</span><span> module name </span><span>*/</span> <span>19</span> myext2_functions, <span>/*</span><span> module functions </span><span>*/</span> <span>20</span> NULL, <span>/*</span><span> module initialize </span><span>*/</span> <span>21</span> NULL, <span>/*</span><span> module shutdown </span><span>*/</span> <span>22</span> NULL, <span>/*</span><span> request initialize </span><span>*/</span> <span>23</span> NULL, <span>/*</span><span> request shutdown </span><span>*/</span> <span>24</span> NULL, <span>/*</span><span> phpinfo </span><span>*/</span> <span>25</span> PHP_MYEXT2_VERSION, <span>/*</span><span> module version </span><span>*/</span> <span>26</span> <span> STANDARD_MODULE_PROPERTIES </span><span>27</span> <span>}; </span><span>28</span> <span>/*</span><span> }}} </span><span>*/</span> <span>29</span> <span>30</span> <span>#ifdef COMPILE_DL_MYEXT2 </span><span>31</span> <span>ZEND_GET_MODULE(myext2) </span><span>32</span> <span>#endif</span> <span>33</span> <span>34</span> <span>/*</span><span> {{{ proto void hello() </span><span>35</span> <span> Print "hello world!" </span><span>*/</span> <span>36</span> <span>PHP_FUNCTION(hello) </span><span>37</span> <span>{ </span><span>38</span> php_printf(<span>"</span><span>hello world!\n</span><span>"</span><span>); </span><span>39</span> <span>} </span><span>40</span> <span>/*</span><span> }}} </span><span>*/</span>
对比下扩展骨架创建的.c文件就会发现,我们的.c文件非常的简单,其实这些对一个最基本的扩展来说就已经足够了。
上面的代码是简单而清晰的,大部分注释已经很具说明性了。我们再简要概括下:
这里面涉及了一些宏,比如PHP_FE,PHP_FE_END,PHP_FUNCTION等等,完整介绍这些宏要到后续的博文中才可以,眼下最简单的办法就是记住这些宏。
注意到我们每一个文件的命名,变量的命名,空格和缩进,以及注释等都是非常规范的,遵循这些规范,可以使我们编写的代码和PHP本身的代码更加契合,我们也推荐你使用这样的规范来开发PHP扩展。
最后,编译运行我们的扩展:
$ ./buildconf --<span>force $ .</span>/configure --help | <span>grep</span><span> myext2 </span>--enable-<span>myext2 Enable myext2 support $ .</span>/configure --disable-all --enable-myext2 --enable-debug --enable-maintainer-<span>zts $ </span><span>make</span><span> $ php</span>-dev -m | <span>grep</span><span> myext2 myext2 $ php</span>-dev -r <span>'</span><span>hello();</span><span>'</span><span> hello world</span>!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
