深入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

相关推荐

  • 文件的压缩与解压

    文件的压缩与解压   常用命令既参数 1, gzip                        …

    Linux干货 2017-04-16
  • 编译安装lamp框架

    一、 先说说啥叫lamp     1. lamp简介: Linux Apache Mysql PHP(Python, Perl)的简称,下面说说他们之间的关系         Linux:系统运行平台    &…

    Linux干货 2015-06-18
  • Linux基础知识

    一、描述计算机的组成及其功能 计算机由运算器、控制器、存储器、I/O设备五大部件组成。 运算器和控制器统称为中央处理器,俗称CPU。    1、运算器:实现算术运算和逻辑运算的部件;    2、控制器:计算机的指挥系统,控制计算机的其他部件,使得个部件有条不紊地协调工作;    3、存储器:存储数据…

    2017-07-08
  • AWK 的用法

    目录: 一、概述 二、awk基本语法格式 三、awk基本操作 四、awk条件及循环语句 五、awk函数 六、awk演示示例(源自于man手册) 一、概述 产品概述:  awk是一种编程语言,用于在linux/unix下对文本和数据进行扫描与处理。数据可以来自标准输入、文件、管道。&nbsp…

    Linux干货 2017-05-30
  • linux用户组和权限管理

    用户组和权限管理  概念 用户分为三类: 1、超级用户:root uid=0,简单来说uid为0的用户就是超级用户。 2、虚拟用户:存在linux中,满足文件或者程序运行的需要,而创建的。不能登录,不能使用。uid=1-499,1-999(centos7) 3、普通用户:管理员root创建的用户。uid=500+,1000+(centos7) 用户…

    Linux干货 2017-07-29
  • MAN 手册各章节功能及快捷键键位介绍

      man命令在linux下属于一种帮助命令,man手册提供了比较齐全的帮助格式,它大致分为8个章节 一.各个章节如下 1 – commands 1-普通的命令 (用户命令,  可由任何人启动的) 2 – system calls 2-系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函…

    Linux干货 2016-10-17