【视频】从零开始编写第一个PHP扩展

Rango会讲解在Linux下从零开始写一个PHP扩展,并编译安装到PHP里,一直到执行扩展中的函数。包含的内容有:

  • 为什么要开发PHP扩展
  • ext_skel工具的使用
  • 修改config.m4
  • phpize的使用
  • 必备工具,autoconf / gcc / make / php5 / php5-dev
  • ZendAPI的使用简介,会介绍几个重要的API

摘自:http://wiki.swoole.com/wiki/page/238.html


学习笔记:

1、安装 Eclipse IDE 插件

(1) 去官网下载 Eclipse 3.7

http://www.eclipse.org/downloads/packages/release/indigo/sr2

(2) 安装 IDE插件

Help -> Install New Software

Work width -> 选择 “Indigo - http://download.eclipse.org/releases/indigo

Programming Languages ->

选择“C/C++ Development Tools
选择“PHP Development Tools(PDT) SDK Feature

(3) 下载PHP源码

http://php.net/releases/

2、安装开发工具(3个)

sudo apt-get install gcc make autoconf
  • gcc:C语言的编译器
  • make:C/C++工程自动构建工具
  • autoconf:检测一些系统环境,以及一些编译参数的设置

3、开发扩展

(1)  重要工具  ./ext/ext_skel

为我们自动创建一个PHP扩展工程的工具/脚本,执行它一下,会打印一些帮助信息

$ ./ext_skel  
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

(2) 利用 ext_skel 创建工程

$ ./ext_skel --extname=test
Creating directory test
Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/test/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-test
5.  $ make
6.  $ ./sapi/cli/php -f ext/test/test.php
7.  $ vi ext/test/test.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/test/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

(3) 进入到 扩展 test 目录中

config.m4:autoconf 工具的配置文件
php_test.h:头文件
test.c:C源文件

开发扩展的第一步,就是修改 config.m4,把我们的扩展启用,dnl 是 autoconf 的 注释符,把

dnl PHP_ARG_WITH(test, for test support,
dnl Make sure that the comment is aligned:
dnl [  --with-test             Include test support])

改为

PHP_ARG_WITH(test, for test support,
[  --with-test             Include test support])

表示 启用了这个扩展

(4) 在 扩展test 目录下,执行命令 

$ /opt/software/php/bin/phpize 
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

PHP根据刚才修改的 config.m4 文件,生成一个 configure 脚本,执行成功后,会看到这个目录就多了一些文件。

我们最主要看的是 configure 这个文件,它是一个Shell脚本,这个脚本帮我们检测一些头文件,一些环境特性方面的东西。

(5) 执行 configure 文件

sudo ./configure  --with-php-config=/opt/software/php/bin/php-config

执行成功后,在扩展目录下会产生一个 Makefile 文件,它是 make 命令的配置文件,

(6) make 根据 Makefile 文件,对 C 源文件进行编译。

$ sudo make

会在扩展的 modules 目录下,生成 test.so 文件

(7) 安装扩展

将刚才编译好的扩展(test.so)安装到PHP的扩增目录

$ sudo make install
Installing shared extensions:     /opt/software/php/lib/php/extensions/no-debug-non-zts-20100525/

(8) 启用扩展

通过如下命令,找到 php.ini 文件位置,

$ /opt/software/php/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /opt/software/php/etc
Loaded Configuration File => /opt/software/php/etc/php.ini

编译 php.ini,添加

extension=test.so

(9) 查看扩展是否被启用

$ /opt/software/php/bin/php -m
[PHP Modules]
bcmath
bz2
...
sysvshm
test
tokenizer
...
zip
zlib

[Zend Modules]
Xdebug

(10) 自己重新写一个函数 test_hello

php_test.h  文件中,增加

PHP_FUNCTION(test_hello);

test.c 文件中,增加

PHP_FUNCTION(test_hello)
{
    long a;
    long b;
    char *c;
    int c_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &a, &b, &c, &c_len) == FAILURE) {
        return;
    }

    char *str;
    int len = spprintf(&str, 0, "%s: %d ", c, a * b);

    RETURN_STRINGL(str, len, 0);
}

再重新编译、安装  sudo make && make install

(11) 检测 自定义函数 test_hello 是否存在

$ php --rf 'test_hello' 
Exception: Function test_hello() does not exist

提示 函数不存在,说明该函数还没有注册到 Zend引擎中

(12) 注册 test_hello函数到 Zend引擎中

在 test.c 文件中找到如下代码块:

/* {{{ test_functions[]
 *
 * Every user visible function must have an entry in test_functions[].
 */
const zend_function_entry test_functions[] = {
    PHP_FE(confirm_test_compiled,    NULL)        /* For testing, remove later. */
    PHP_FE_END    /* Must be the last line in test_functions[] */
};

增加 test_hello 函数的注册

const zend_function_entry test_functions[] = {
    PHP_FE(test_hello,    NULL)
    PHP_FE(confirm_test_compiled,    NULL)        /* For testing, remove later. */
    PHP_FE_END    /* Must be the last line in test_functions[] */
};

(13) 重新 编译、安装

$ sudo make && make install

(14) 重新 检测函数是否存在

$ php --rf 'test_hello'
Function [ <internal:test> function test_hello ] {
}

OK,函数注册Zend成功!

原文地址:https://www.cnblogs.com/52php/p/5681560.html