PHP内核的学习--建立PHP扩展

开始看PHP内核也有一段时间了,如今开始边学边总结,今天就总结一下如何建立本身的PHP扩展。php

个人环境以下:html

系统:Ubuntu 14.04web

php版本:5.5.19数据库

参考摘录:用C/C++扩展你的PHPsvn

PHP取得成功的一个主要缘由之一是它拥有大量的可用扩展。web开发者不管有何种需求,这种需求最有可能在PHP发行包里找到。PHP发行包包括支持各类数据库,图形文件格式,压缩,XML技术扩展在内的许多扩展。函数

扩展API的引入使PHP3取得了巨大的进展,扩展API机制使PHP开发社区很容易的开发出几十种扩展。如今,两个版本过去了,API仍然和PHP3时的很是类似。扩展主要的思想是:尽量的从扩展编写者那里隐藏PHP的内部机制和脚本引擎自己,仅仅须要开发者熟悉API。php-fpm

有两个理由须要本身编写PHP扩展。第一个理由是:PHP须要支持一项她还未支持的技术。这一般包括包裹一些现成的C函数库,以便提供PHP接口。例如,若是一个叫FooBase的数据库已推出市场,你须要创建一个PHP扩展帮助你从PHP里调用FooBase的C函数库。这个工做可能仅由一我的完成,而后被整个PHP社区共享(若是你愿意的话)。第二个不是很广泛的理由是:你须要从性能或功能的缘由考虑来编写一些商业逻辑。性能

假设你正在开发一个网站,须要一个把字符串重复n次的函数。下面是用PHP写的例子:测试

function util_str_repeat($string, $n){
    $result = "";
    for($i = 0; $i < $n; $i++){
        $result .= $string;
    }
    return $result;
}
 
util_str_repeat("One", 3);// returns "OneOneOne".
util_str_repeat("One", 1);// returns "One".

假设因为一些奇怪的缘由,你须要时常调用这个函数,并且还要传给函数很长的字符串和大值n。这意味着在脚本里有至关巨大的字符串链接量和内存从新分配过程,以致显著地下降脚本执行速度。若是有一个函数可以更快地分配大量且足够的内存来存放结果字符串,而后把$string重复n次,就不须要在每次循环迭代中分配内存。网站

为扩展创建函数的第一步是写一个函数定义文件,该函数定义文件定义了扩展对外提供的函数原形。该例中,定义函数只有一行函数原形util_str_repeat() :

string util_str_repeat(string str, int n)

函数定义文件的通常格式是一个函数一行。你能够定义可选参数和使用大量的PHP类型,包括: bool, float, int, array等。

保存为util.def文件至PHP原代码目录树下(即与ext_skel文件放在同一目录下,个人目录是/usr/share/php5/)。

而后就是经过扩展骨架(skeleton)构造器运行函数定义文件的时机了。该构造器脚本就是ext_skel。假设你把函数定义保存在一个叫作util.def的文件里,并且你但愿把扩展取名为util,运行下面的命令来创建扩展骨架:

sudo ./ext_skel --extname=util --proto=util.def

执行以后,我这里报了以下错误:

./ext_skel: 1: cd: can't cd to /usr/lib/php5/skeleton
Creating directory util
awk: cannot open /create_stubs (No such file or directory)
Creating basic files: config.m4 config.w32 .svnignore util.c./ext_skel: 216: ./ext_skel: cannot open /skeleton.c: No such file
 php_util.h./ext_skel: 234: ./ext_skel: cannot open /php_skeleton.h: No such file
 CREDITS./ext_skel: 238: ./ext_skel: cannot open /CREDITS: No such file
 EXPERIMENTAL./ext_skel: 242: ./ext_skel: cannot open /EXPERIMENTAL: No such file
 tests/001.phpt./ext_skel: 247: ./ext_skel: cannot open /tests/001.phpt: No such file
 util.php./ext_skel: 251: ./ext_skel: cannot open /skeleton.php: No such file
rm: cannot remove ‘function_entries’: No such file or directory
rm: cannot remove ‘function_declarations’: No such file or directory
rm: cannot remove ‘function_stubs’: No such file or directory
 [done].

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

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

Repeat steps 3-6 until you are satisfied with ext/util/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.

很明显是/usr/lib/php5/skeleton路径的错误,编辑ext_skel文件,将/usr/lib/php5/skeleton修改成/usr/share/php5/skeleton,而后移除掉生成的util文件夹,再次执行以前的命令,成功后提示以下:

Creating directory util
Creating basic files: config.m4 config.w32 .svnignore util.c php_util.h CREDITS EXPERIMENTAL tests/001.phpt util.php [done].

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

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

Repeat steps 3-6 until you are satisfied with ext/util/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.

而后采用静态编译的方式编译扩展。为了使扩展可以被编译,须要修改扩展目录util/下的config.m4文件。扩展没有包裹任何外部的C库,你须要添加支持–enable-util配置开关到PHP编译系统里(–with-extension 开关用于那些须要用户指定相关C库路径的扩展)。找到以下内容:

dnl PHP_ARG_ENABLE(util, whether to enable util support,
dnl Make sure that the comment is aligned:
dnl [  --enable-util           Enable util support])

将前面的dnl 去掉,修改成以下结果:

PHP_ARG_ENABLE(util, whether to enable util support,
Make sure that the comment is aligned:
[  --enable-util           Enable util support])

而后修改util.c文件,找到以下代码:

PHP_FUNCTION(util_str_repeat)
{
    char *str = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_len;
    long n;

    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE) 
        return;

    php_error(E_WARNING, "util_str_repeat: not yet implemented");
}

将其修改成以下代码:

PHP_FUNCTION(util_str_repeat)
{
    char *str = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_len;
    long n;
    char *result; /* Points to resulting string */
    char *ptr; /* Points at the next location we want to copy to */
    int result_length; /* Length of resulting string */

    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        return;

    /* Calculate length of result */
    result_length = (str_len * n);
    /* Allocate memory for result */
    result = (char *) emalloc(result_length + 1);
    /* Point at the beginning of the result */
    ptr = result;

    while (n--) {
        /* Copy str to the result */
        memcpy(ptr, str, str_len);
        /* Increment ptr to point at the next position we want to write to */
        ptr += str_len;
    }
/* Null terminate the result. Always null-terminate your strings even if they are binary strings */ *ptr = '\0'; /* Return result to the scripting engine without duplicating it*/ RETURN_STRINGL(result, result_length, 0); }

里面的具体内容,就不在这里说了,以后会慢慢写到。

而后就是编译,安装。在util目录下,命令以下(命令可能都须要加sudo):

phpize
./configure
make
make test
make install

而后配置生成的扩展文件,在php5.5版本中,进入到/etc/php5/mods-available目录下,建立util.ini文件,写入以下内容:

extension=util.so

而后enable util扩展

sudo php5enmod util

最后,重启php-fpm

sudo service php5-fpm restart

建立一个php文件,测试一下,测试文件以下:

<?php
for ($i = 1; $i <= 3; $i++) {
    print util_str_repeat("CraryPrimitiveMan ", $i);
    print "\n";
}
?>

执行结果以下:

CraryPrimitiveMan 
CraryPrimitiveMan CraryPrimitiveMan 
CraryPrimitiveMan CraryPrimitiveMan CraryPrimitiveMan

这样咱们就成功建立了一个包含简单的PHP函数的扩展。

盗图一张~~

今天就先到这里~~

相关文章
相关标签/搜索