PHP extension Development (1): Getting Started, PHP extension _php Tutorials

Source: Internet
Author: User
Tags php source code sapi zts

PHP extension Development (1): Getting Started, PHP extensions


PHP extension Development of the article, blog has been many, more classic is:

I'm going to summarize my learning and understanding of PHP Extension Development in this series, trying to simply and clearly describe the basics of developing a PHP extension in a Linux system. The level is low, inevitably has the mistake, hoped to point out.

Preparatory work

Start by getting a copy of the PHP source code (you can check it out on GitHub, or download the latest stable version on the official website) and compile it. To speed up the compilation, we recommend disabling all additional extensions (using the--disable-all option), but it's a good idea to turn on debug (using the--enable-debug option) and thread safety (using--ENABLE-MAINTAINER-ZTS). However, to turn off debug when publishing the extension, choose whether to turn on thread safety as appropriate:

$./buildconf--force$. /configure--disable-all--enable-debug--enable-maintainer- make

Note that we did not specify the--prefix option (and also no make install), because this is not required. Note that if you look at the output information, you may need to install some dependent packages to successfully compile PHP.

Compiled PHP executable program in the source code of the SAPI directory, corresponding to different host environments have different subdirectories, we will use the CLI (Command line interface) environment, we can build an alias for easy reference:

$ alias php-dev=/usr/local/src/php-5.6. 5/sapi/cli/php

There are a few command-line options that are useful:

Php-dev-H # Print help information php-dev-v # print version info php-dev--INI # Print configuration information        PHP-D EV-m # Print loaded module information PHP-dev-I # phpinfophp      # 执行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 --force$ .grep myext    --enable-myext           Enable myext support$ ./configure --disable-all --enable-myext --enable-debug --enable-maintainer-zts$ make

注意,我们用./configure --help | grep myext打印了我们扩展的加载情况,如果看不到下面的输出,则说明我们的扩展没有配置成功,回头检查下config.m4文件。

这次编译应该非常快,因为大部分代码都已经编译过了。PHP还有另外一种编译扩展的方法(使用动态连接的方式,将扩展编译为.so的文件),不过我们推荐在开发扩展的时候使用静态编译,因为这样省去了在配置文件中加载扩展的步骤。

一切顺利的话,我们的第一个扩展就已经可以执行了:

grep myextmyext$ php'echo confirm_myext_compiled("myext") . "\n";'Congratulations! 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,[  --enable-myext2           Enable myext2 support])if test "PHP_MYEXT2" != "no"; then   PHP_NEW_EXTENSION(myext2, myext2.c, $ext_shared)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:

 1#ifndef PHP_MYEXT2_H 2#define PHP_MYEXT2_H 3 4extern zend_module_entry myext2_module_entry; 5#define phpext_myext2_ptr &myext2_module_entry 6 7#define PHP_MYEXT2_VERSION "0.1.0" 8 9/* prototypes */10PHP_FUNCTION(hello);1112#endif  /* PHP_MYEXT2_H */

这里主要的代码是定义了名为phpext_myext2_ptr的宏,PHP底层通过该宏来引用我们的扩展。可以看出,该宏的命名同样是有规范的 — phpext_扩展名_ptr。而myext2_module_entry是我们稍后要在.c文件里定义的结构体,它的命名也是规范的 — 扩展名_module_entry。

此外我们还定义了一个标识我们扩展版本号的宏和一个函数原型(通过PHP_FUNCTION宏,PHP_FUNCTION宏函数的参数是外部可使用的函数名),稍后我们会来实现这个函数。

最后来看下myext2.c文件的实现:

1#include"Php.h"2#include"php_myext2.h"3 4 /*{{{myext2_functions[]5  *6 * Every user visible function must has a entry in myext2_functions[].7  */8 Static ConstZend_function_entry myext2_functions[] = {9 Php_fe (Hello, NULL)Ten Php_fe_end One }; A /* }}} */ -  - /*{{{ myext2_module_entry the  */ -Zend_module_entry Myext2_module_entry = { - Standard_module_header, -     "myext2",/*Module Name*/ +Myext2_functions,/*module Functions*/ -Null/*Module Initialize*/ +Null/*Module Shutdown*/ ANull/*Request Initialize*/ atNull/*Request Shutdown*/ -Null/*Phpinfo*/ -Php_myext2_version,/*Module Version*/ - standard_module_properties - }; - /* }}} */ in  - #ifdef COMPILE_DL_MYEXT2 to zend_get_module (MYEXT2) + #endif -  the /*{{{proto void Hello () * Print "Hello world!"*/ $ php_function (Hello)Panax Notoginseng { -php_printf ("Hello world!\n"); the } + /* }}} */

对比下扩展骨架创建的.c文件就会发现,我们的.c文件非常的简单,其实这些对一个最基本的扩展来说就已经足够了。

上面的代码是简单而清晰的,大部分注释已经很具说明性了。我们再简要概括下:

这里面涉及了一些宏,比如PHP_FE,PHP_FE_END,PHP_FUNCTION等等,完整介绍这些宏要到后续的博文中才可以,眼下最简单的办法就是记住这些宏。

注意到我们每一个文件的命名,变量的命名,空格和缩进,以及注释等都是非常规范的,遵循这些规范,可以使我们编写的代码和PHP本身的代码更加契合,我们也推荐你使用这样的规范来开发PHP扩展。

最后,编译运行我们的扩展:

$ ./buildconf --force$ .grep myext2  --enable-myext2           Enable myext2 support$ ./configure --disable-all --enable-myext2 --enable-debug --enable-maintainer-zts$ make$ phpgrep myext2myext2$ php'hello();'hello world!

http://www.bkjia.com/PHPjc/960240.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/960240.htmlTechArticlePHP扩展开发(1):入门,php扩展 有关PHP扩展开发的文章、博客已经很多了,比较经典的有: 我准备在此系列博文中总结我有关PHP扩展开发...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.