PHP扩展开发 第一课 为什么要写扩展及hello world

PHP扩展开发其实很简单.那为什么要扩展开发呢. 这里咱们以实际的案例进行对比.
第一步,进入 php源码包   http://www.php20.com/forum.php?m ... =159&extra=page%3D1  可以从这里下载
第二步,进入源码包的ext目录 .如

[Shell] 纯文本查看 复制代码
1
cd /lamp_source/php-5.6.23/ext;


第三步,执行命令  

[Shell] 纯文本查看 复制代码
1
./ext_skel   --extname=hw
./ext_skel   --extname=hw Creating directory hw
Creating basic files: config.m4 config.w32 .gitignore hw.c php_hw.h CREDITS EXPERIMENTAL tests/001.phpt hw.php [done].
To use your new extension, you will have to execute the following steps:

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


第四步,按提示   进入扩展目录   

[Shell] 纯文本查看 复制代码
1
cd /lamp_source/php-5.6.23/ext/hw;

然后vi hw.c  修改代码如下
搜索 PHP_FUNCTION  找到 PHP_FUNCTION(confirm_hw_compiled)这行代码. 在这个函数前增加以下两个函数

[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
PHP_FUNCTION(add1)
{
        int i, rs;
        for(i = 0; i < 10000000; i++)
        {
                rs = i + 1;
        }
        RETURN_LONG(rs);
}
PHP_FUNCTION(multiplication1)
{
        int i,rs;
        for(i = 0;i < 10000000; i++)
        {
                rs = i * 2;
        }
        RETURN_LONG(rs);
}

保存,
再搜索 PHP_FE找到下面的代码

[C] 纯文本查看 复制代码
1
2
3
const zend_function_entry hw_functions[] = {
        PHP_FE(confirm_hw_compiled,     NULL)           /* For testing, remove later. */
        PHP_FE_END      /* Must be the last line in hw_functions[] */



中间加上  
        PHP_FE(add1,NULL)
        PHP_FE(multiplication1,NULL)

结果 如下

[C] 纯文本查看 复制代码
1
2
3
4
5
const zend_function_entry hw_functions[] = {
        PHP_FE(confirm_hw_compiled,     NULL)           /* For testing, remove later. */
        PHP_FE(add1,NULL)
        PHP_FE(multiplication1,NULL)
        PHP_FE_END      /* Must be the last line in hw_functions[] */




然后执行phpize 比如您的php路径是
sh-3.2# /usr/local/php-5.6.23/  则是  /usr/local/php-5.6.23/bin/phpize
出现以下则正常

Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226


再然后,配置安装 .代码如下

[Shell] 纯文本查看 复制代码
1
./configure --with-php-config=/usr/local/php-5.6.23/bin/php-config

要注意php路径.跟上面的phpize是同一个目录   回车如果不报错则正常.
再输入两个命令 make && make install 如果看到下面的代码说明正常

Installing shared extensions:     /usr/local/php-5.6.23/lib/php/extensions/no-debug-non-zts-20131226/


接下来 修改一下php.ini  增加extension=hw.so
sh-3.2# vi /usr/local/php-5.6.23/php.ini  搜索 extension 可以找的到

重启php-fpm  然后phpinfo()查看一下.如图

第五步.测试扩展到底有个毛用?php代码如下

[PHP] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
function add()//加法函数
{
    $rs = 0;
    for ($i = 0 ; $i < 10000000; $i++)
    {
        $rs = $i +1;
    }
    return $rs;
}
function multiplication()//乘法函数
{
    $rs = 0;
    for ($i = 0 ; $i < 10000000; $i++)
    {
        $rs = $i * 2;
    }
    return $rs;
}
$time = microtime(true);//取微秒
echo add();//运行函数
$add1 = microtime(true) - $time;//计算函数占用时间.这是php的加法函数时间,add1是结果
echo 'add time:' . $add1 . '<hr/>';//现实出来
$time = microtime(true);//同上.计算php的乘法时间
echo multiplication();
$multiplication1 = microtime(true) - $time;//结果
echo 'multiplication time:' . $multiplication1 . '<hr/>';
$time = microtime(true);//取扩展前的时间
echo add1();//扩展函数
$add2 = microtime(true) - $time;//结果
echo 'ext add time:' . $add2 . '<hr/>';
$time = microtime(true);
echo multiplication1();//相同.这是扩展乘法
$multiplication2 = microtime(true) - $time;
echo 'ext multiplication time:' . $multiplication2 . '<hr/>';//结果
 
echo 'add1 / add2 = '  . ($add1 / $add2) . '<hr>';//扩展是php的多少倍
echo 'multiplication1 / multiplication2 = '  . ($multiplication1 / $multiplication2) . '<hr>';





最后查看结果....

加法大约是126万倍(平均在60-120万倍) 乘法大约是126万倍(平均在100万倍以上)

原文地址:https://www.cnblogs.com/ghjbk/p/6635668.html