深入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 19:57
下一篇 2016-10-29 20:15

相关推荐

  • 到处都是Unix的胎记

    一说起Unix编程,不必多说,最著名的系统调用就是fork,pipe,exec,kill或是socket了(fork(2),execve(2), pipe(2), socketpair(2), select(2), kill(2), sigaction(2))这些系统调用都像是Unix编程的胎记或签名一样,表…

    Linux干货 2015-04-03
  • nginx_http_proxy,upstream,stream模块简析

    一. ngx_http_proxy_module模块:         模块功能: 为后端httpd服务做反向代理, 并且与Httpd 之间使用http进行通信       1、proxy_pass URL;  &nbs…

    Linux干货 2016-10-29
  • N25期–第十七周作业

    1、  结合图形描述LVS的工作原理; 针对高可伸缩、高可用网络服务的需求,给出了基于IP层和基于内容请求分发的负载平衡调度解决方法,并在Linux内核中实现了这些方法,将一组服务器构成一个实现可伸缩的、高可用网络服务的虚拟服务器。 虚拟服务器的体系结构如图所示,一组服务器通过高速的局域网或者地理分布的广域网相互连接,在它们的前端有一个负载调度器(…

    2017-05-08
  • inode 是什么及绝对路径和相对路径和一些目录相关命令总结

    inode是什么          每个分区又分两个区,元数据区和数据区。     元数据区主要存储文件的权限,属主属组,时间戳,文件的存储位置指针….就如一本书有目录页和内容页一样,目录页上的记录的是内容的页数,可以通过目录页的页数直接…

    Linux干货 2016-08-02
  • 简述计算机硬件组成,linux系统及简单命令详解

    1、计算机的硬件组成 (1)cpu(中央处理器):主要由运算器和控制器组成,它的运算器主要负责程序运算与逻辑判断,控制器则主要协调组件与各个设备之间工作。 (2)存储设备:存储设备又分为外存储设备,是用来存储数据信息的设备,它包括,硬盘,u盘,光盘,软盘。而其中内存(ram)和主板上的rom是属于内存储设备,内存是只读存储器,它用来暂时记录数据信息供给cpu…

    2017-07-09
  • exit和退出码

    用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。   常用参数 格式:exitn 退出。设置退出码为n。(Causethe shell to exit with a status of n.)   格式:exit 退出。退出码不变,即为最后一个命令的退出码。(Ifn is omitted, the e…

    Linux干货 2016-01-14