深入Php底层,用c为php编写拓展

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=helloWorld
Creating directory helloWorld
Creating 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.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-helloWorld
5.  $ make
6.  $ ./sapi/cli/php -f ext/helloWorld/helloWorld.php
7.  $ vi ext/helloWorld/helloWorld.c
8.  $ make

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

            

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

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> ll
total 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.h
drwxr-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/phpize
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212

        生成拓展:

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

[root@localhost~/tools/php-5.5.38/ext/helloWorld]> ll
total 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.m4
drwxr-xr-x. 2 root chuji001   4096 Oct 30 03:39 autom4te.cache
drwxr-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]> ll
total 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编写拓展是一个深入的课题,笔者这里只是给出了简单的例子,如读者有需要,需找更加详细的资料,本文的目的仅为入门。

原创文章,作者:21期王逸凡,如若转载,请注明出处:http://www.178linux.com/55578

(0)
21期王逸凡21期王逸凡
上一篇 2016-10-29
下一篇 2016-10-29

相关推荐

  • Linux磁盘管理高级

    Linux磁盘管理高级 LVM Btrsh LVM 逻辑卷管理(LVM)指系统将物理卷管理抽象成一个逻辑卷,包括重新设定文件系统的大小,LVM可以弹性的更改LVM的容量,通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量。 LVM还有快照的功能,快照区与原本的LV共用很多PE的区块,因…

    Linux干货 2016-09-09
  • linux网络管理(基础-IP、MAC、TCP)

    linux网络管理 计算机网络 MACMAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制、介质访问控制,或称为物理地址、硬件地址,用来定义网络设备的位置。在OSI模型中,第三层网络层负责IP地址,第二层数据链路层则负责MAC地址。因此一个主机会有一个MAC地址,而每个网络位置会有一个专属于…

    Linux干货 2016-09-09
  •  编译安装步骤     大致过程:(源代码–预处理–编译–汇编–链接–执行)   第一步 下载安装开发工具“Development toos” 第二步 下载解压源代码包 仔细阅读“Install和 Read me” 第三步 使用./conf…

    Linux干货 2017-04-24
  • Linux基础之—用户和组管理

    导言:   我们在使用计算机资源时为了资源的合理规划和利用,同时为了实现更好的管理,需要在计算机里有一个使用资源的凭证,这个凭证就是用户,下面就来介绍Linux用户和组管理。 1.资源分派:     资源的分派可以分为三个步骤,分别是    (1)认证(Authertication)   &nbsp…

    Linux干货 2016-08-04
  • 9、varnish实现动静分离、负载均衡、后端服务器健康状态检测

    varnish 4.0 版本 varnish实现动静分离 # vim /etc/varnish/varnish.params VARNISH_LISTEN_PORT=80 –> 修改为80端口 # vim /etc/varnish/default.vcl backend html {    .host = “1…

    Linux干货 2016-11-13