用c语言为php编写拓展

来源:互联网 发布:淘宝鬼脚七微博 编辑:程序博客网 时间:2024/06/17 09:13

1.前言

    

        随着lamp/lnmp架构的流行,Php语言越来越得到广泛的使用。php语言在表现层有着非常优异的表现,部署方便,开发迅速。但Php语言也有着天生短板以及局限性----对多线程以及多进程的支持不甚如意,以及相对于静态语言缓慢的执行速度。如今网站的数据越来越多,涉及到的密集型计算对性能的要求越来越高,php语言越来越难以满足这种计算性能的要求,由于Php底层由c语言开发完成,用c语言更改php底层以及为Php语言编写底层拓展是解决Php本身性能的一大方法。


        本文将以一个简单实例来演示如何为Php编写底层拓展,读者掌握此方法后,可以根据具体的业务需求,为php编写具有良好性能的底层拓展。如,笔者在工作中遇到过随时计算用户之间GPS距离的产品需求,涉及的计算量巨大。读者遇到此类的需求,可以考虑在底层专门写出c语言拓展来解决问题。本文成稿时间匆忙,纰漏之处在所难免,希望读者指正。


2.编写拓展

   2.1 自动生成框架

         

        请下载php源码包,yum安装的php没有ext_skel这个文件。ext_skel这个文件就用来帮我们生成框架。具体的目录如下:

[root@localhost~/tools/php-5.5.38/ext]> ll /root/tools/php-5.5.38/ext/ext_skel-rwxr-xr-x. 1 1000 admins 8192 Jul 20 16:41 /root/tools/php-5.5.38/ext/ext_skel

             现在执行这个文件,生成一个helloWorld的框架:

[root@localhost~/tools/php-5.5.38/ext]> /root/tools/php-5.5.38/ext/ext_skel --extname=helloWorldCreating directory helloWorldCreating basic files: config.m4 config.w32 .svnignore helloWorld.c php_helloWorld.h CREDITS EXPERIMENTAL tests/001.phpt helloWorld.php [done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/helloWorld/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-helloWorld5.  $ make6.  $ ./sapi/cli/php -f ext/helloWorld/helloWorld.php7.  $ vi ext/helloWorld/helloWorld.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/helloWorld/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

            

如上所示,ext_skel帮我们生成了helloWorld拓展的框架,里面包含了一个配置文件config.m4, c语言的头文件php_helloWorld.h, 以及函数文件helloWorld.c(这个文件就是写入c函数体的地方)。找到这些文件:

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> lltotal 32-rw-r--r--. 1 root chuji001 2178 Oct 30 02:54 config.m4-rw-r--r--. 1 root chuji001  324 Oct 30 02:54 config.w32-rw-r--r--. 1 root chuji001   10 Oct 30 02:54 CREDITS-rw-r--r--. 1 root chuji001    0 Oct 30 02:54 EXPERIMENTAL-rw-r--r--. 1 root chuji001 5296 Oct 30 02:54 helloWorld.c-rw-r--r--. 1 root chuji001  514 Oct 30 02:54 helloWorld.php-rw-r--r--. 1 root chuji001 2962 Oct 30 02:54 php_helloWorld.hdrwxr-xr-x. 2 root chuji001 4096 Oct 30 02:54 tests


    2.2 编辑文件

    编辑config.m4这个配置文件,找到如下配置行,去掉前面的dnl, 目的是方便之后configure编译新开发出来的模块:

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

     改为:

 10 PHP_ARG_WITH(helloWorld, for helloWorld support, 11 Make sure that the comment is aligned: 12 [  --with-helloWorld             Include helloWorld support])


接下来编写c的头文件,找到这一行:

 PHP_FUNCTION(confirm_helloWorld_compiled);      /* For testing, remove later. */

这一行是我们实际调用的函数名称,读者可以改成方便开发团队调用的名称,也可以不更改,这里笔者更改如下:

PHP_FUNCTION(helloWorld);       /* For testing, remove later. */


接下来更改编译文件helloWorld.c函数文件:

 41 const zend_function_entry helloWorld_functions[] = { 42         PHP_FE(confirm_helloWorld_compiled,     NULL)           /* For testing, remove late    r. */ 43         PHP_FE_END      /* Must be the last line in helloWorld_functions[] */ 44 };

改为注册好的函数名:

 41 const zend_function_entry helloWorld_functions[] = { 42         PHP_FE(helloWorld,     NULL)           /* For testing, remove late    r. */ 43         PHP_FE_END      /* Must be the last line in helloWorld_functions[] */ 44 };



接下来就是编写自己的业务逻辑函数,笔者这里写入一个简单的helloWorld,读者可根据自己的需求来编写调用的函数:

154 PHP_FUNCTION(helloWorld)155 {156     zend_printf("hello world\n");157 }


2.2 编译生成拓展


      编译php:

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> /application/php5.5.34/bin/phpizeConfiguring for:PHP Api Version:         20121113Zend Module Api No:      20121212Zend Extension Api No:   220121212

        生成拓展:

 这个时候观察目录,多了一个configure编译文件

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> lltotal 1244-rw-r--r--. 1 root chuji001  79850 Oct 30 03:39 acinclude.m4-rw-r--r--. 1 root chuji001 310225 Oct 30 03:39 aclocal.m4drwxr-xr-x. 2 root chuji001   4096 Oct 30 03:39 autom4te.cachedrwxr-xr-x. 2 root chuji001   4096 Oct 30 03:39 build-rwxr-xr-x. 1 root chuji001  45079 Oct 30 03:39 config.guess-rw-r--r--. 1 root chuji001   1542 Oct 30 03:39 config.h.in-rw-r--r--. 1 root chuji001   2166 Oct 30 03:09 config.m4-rwxr-xr-x. 1 root chuji001  35782 Oct 30 03:39 config.sub-rwxr-xr-x. 1 root chuji001 447850 Oct 30 03:39 configure

执行configure编译,生成拓展, configure完成之后执行make(必要):

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> ./configure --with-php-config=/application/php5.5.34/bin/php-config

 查看array_square_sum目录的module目录,会发现里面生成helloWorld.so,这个就是我们需要的扩展。

[root@localhost~/tools/php-5.5.38/ext/helloWorld/modules]> lltotal 28-rw-r--r--. 1 root chuji001   823 Oct 30 03:46 helloWorld.la-rwxr-xr-x. 1 root chuji001 23351 Oct 30 03:46 helloWorld.so

编写php.ini的配置文件,为Php加入拓展:

 899 extension=helloWorld.so

将生成的拓展文件复制放入php默认的拓展文件目录当中去:

[root@localhost~/tools/php-5.5.38/ext/helloWorld/modules]> cp helloWorld.so /application/php5.5.34/lib/php/extensions/no-debug-zts-20121212/

重启httpd服务器,这个时候通过phpinfo你就可以看到自己编写的拓展啦!如图:


extension.png


编写Php脚本即可直接调用这个函数:

  1 <?php  2    helloWorld();  3 ?>


显示如图:

hello.png



结语

    用c来为Php编写拓展是一个深入的课题,笔者这里只是给出了简单的例子,如读者有需要,需找更加详细的资料,本文的目的仅为入门。

0 0
原创粉丝点击