监理公司管理系统 | 工程企业管理系统 | OA系统 | ERP系统 | 造价咨询管理系统 | 工程设计管理系统 | 签约案例 | 购买价格 | 在线试用 | 手机APP | 产品资料
X 关闭
南昌网站建设公司

当前位置:工程项目OA系统 > 泛普各地 > 江西OA系统 > 南昌OA系统 > 南昌网站建设公司

php程序中需要用到的C代码

申请免费试用、咨询电话:400-8352-114

南昌网站建设

 

在php顺序中需求用到C代码,应该是下面两种状况:
1 已有C代码,在php顺序中想直接用
2 因为php的功能问题,需求用C来完成局部功用


针对第一种状况,最适宜的办法是用system挪用,把现有C代码写成一个自力的顺序。参数经过敕令行或许规范输入传入,后果从规范输出读出。其次,稍费事一点的办法是C代码写成一个daemon,php顺序用socket来和它进行通信。


重点讲讲第二种状况,固然沿用system挪用的办法也可以,然则想想你的目标是优化功能,那么频频的起这么多历程,当然会让功能下降。而写daemon的办法虽然可行,可是繁琐了良多。


我的简略测试,相同一个算法,用C来写比用php效率能进步500倍。而用php扩展的方法,也能进步90多倍(个中的功能损掉在了参数传递上了吧,我猜)。


所以有些时分php扩展就是我们的最佳选择了。


这里我着重引见一下用C写php扩展的办法,并且不需求从新编译php。


起首,找到一个php的源码,php4或许php5版本的都可以,与你目的平台的php版本没有关系。


在源码的ext目次下可以找到名为ext_skel的剧本(windows平台运用ext_skel_win32.php)
在这个目次下执行./ext_skel --extname=hello(我用hello作为例子)
这时生成了一个目次 hello,目次下有几个文件,你只需求关怀这三个:config.m4 hello.c php_hello.h


把这个目次拷备就任何你但愿的当地,cd进去,顺次执行
phpize
./configure
make
什么也没发作,对吧?
这是由于漏了一步,翻开config.m4,找到下面
dnl If your extension references something external, use with:

...
dnl Otherwise use enable:

...
这是让你选择你的扩展运用with照样enable,我们用with吧。把with那一局部作废注释。
假如你和我一样运用vim编纂器,你就会很轻易发现dnl三个字母本来是透露表现注释的呀(这是由于vim默许带了各类文件花样的语法着色包)


我们修正了config.m4后,持续
phpize
./configure
make
这时,modules下面会生成hello.so和hello.la文件。一个是动态库,一个是静态库。

 

你的php扩展曾经做好了,虽然它还没有完成你要的功用,我先说说怎样运用这个扩展吧!ext_skel为你生成了一个hello.php里面有挪用示例,然则阿谁例子需求你把hello.so拷贝到php的扩展目次中去,我们只想完成本人的功用,不想打造盗窟版php,改用我下面的办法来加载吧:
以下为援用的内容:

1.if(!extension_loaded("hello")) {
2.        dl_local("hello.so");
3.}
4.function dl_local( $extensionFile ) {
5.        //make sure that we are ABLE to load libraries
6.        if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
7.                die( "dh_local(): Loading extensions is not permitted.\n" );
8.        }
9.10.        //check to make sure the file exists
11.        if( !file_exists(dirname(__FILE__) . "/". $extensionFile ) ) {
12.                die( "dl_local(): File '$extensionFile' does not exist.\n" );
13.        }
14.15.        //check the file permissions
16.        if( !is_executable(dirname(__FILE__) . "/". $extensionFile ) ) {
17.                die( "dl_local(): File '$extensionFile' is not executable.\n" );
18.        }
19.20.        //we figure out the path
21.        $currentDir = dirname(__FILE__) . "/";
22.        $currentExtPath = ini_get( "extension_dir" );
23.        $subDirs = preg_match_all( "/\//" , $currentExtPath , $matches );
24.        unset( $matches );
25.26.        //lets make sure we extracted a valid extension path
27.        if( !(bool)$subDirs ) {
28.                die( "dl_local(): Could not determine a valid extension path [extension_dir].\n" );
29.        }
30.31.        $extPathLastChar = strlen( $currentExtPath ) - 1;
32.33.        if( $extPathLastChar == strrpos( $currentExtPath , "/" ) ) {
34.                $subDirs--;
35.        }
36.37.        $backDirStr = ""; 
38.        for( $i = 1; $i <= $subDirs; $i++ ) {
39.                $backDirStr .= "..";
40.                if( $i != $subDirs ) {
41.                  $backDirStr .= "/";
42.                }
43.        }
44.45.        //construct the final path to load
46.        $finalExtPath = $backDirStr . $currentDir . $extensionFile;
47.48.        //now we execute dl() to actually load the module
49.        if( !dl( $finalExtPath ) ) {
50.                die();
51.        }
52.53.        //if the module was loaded correctly, we must bow grab the module name
54.        $loadedExtensions = get_loaded_extensions();
55.        $thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];
56.57.        //lastly, we return the extension name
58.        return $thisExtName;
59.60.}//end dl_local()
 

如许的益处是你的php扩展可以随你的php代码走,绿色扩展。


随后一个让人关怀的问题是,若何添加函数、完成参数传递和返回值


添加函数步调如下:
php_hello.h:
PHP_FUNCTION(confirm_hello_compiled);// 括号里面填写函数名

hello.c
zend_function_entry hello_functions[] = {
    PHP_FE(confirm_hello_compiled,  NULL)       /* 这里添加一行 */
    {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */
};
PHP_FUNCTION(confirm_hello_compiled)
{// 这里写函数体
}
要完成的函数原型其实都一个样,用宏PHP_FUNCTION来包装了一下,别的呢,在hello_functions里面添加了一行信息,透露表现你这个模块中有这个函数了。


那么都是一样的函数原型,若何区分返回值与参数呢?
我给一个例子:
以下为援用的内容:

1.PHP_FUNCTION(hello_strdiff)
2.{
3.    char *r1 = NULL, *r2 = NULL;
4.    int n = 0, m = 0;
5.6.    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m) == FAILURE) {
7.        return;
8.    }
9.    while(n && m && *r1 == *r2) {
10.        r1++;
11.        r2++;
12.        n--;
13.        m--;
14.    }
15.16.    if(n == 0) RETURN_LONG(m);
17.    if(m == 0) RETURN_LONG(n);
18.    int d[n+1][m+1];
19.    int cost;
20.    int i,j;
21.    for(i = 0; i <= n; i++) d[i][0] = i;
22.    for(j = 0; j <= m; j++) d[0][j] = j;
23.    for(i = 1; i <= n; i++) {
24.        for(j = 1; j <= m; j++) {
25.            if(r1[i-1] == r2[j-1]) cost = 0;
26.            else cost = 1;
27.            int a = MIN(d[i-1][j]+1,d[i][j-1]+1);
28.            a = MIN(a, d[i-1][j-1]+cost);
29.            d[i][j] = a;
30.        }
31.    }
32.    RETURN_LONG(d[n][m]);
33.}
 

这是一个求两个字符串差别度的算法,输入参数两个字符串,返回整型。
参数的传递看这里
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m)

 

把这个当成是scanf来了解好了。
类型阐明见下表:
Boolean b zend_bool
Long l long
Double d double
String s char*, int
Resource r zval*
Array a zval*
Object o zval*
zval z zval*
假如想完成可选参数的话,例如一个字符串,一个浮点,再加一个可选的bool型,可以用"sd|b"来透露表现。
和scanf有一点分歧的是,关于字符串,你要供应两个变量来存储,一个是char *,存字符串的地址,一个int,来存字符串的长度。如许有需要的时分,你可以平安的处置二进制数据。


那么返回值怎样办呢?
运用下面一组宏来透露表现:
RETURN_STRING

RETURN_LONG

RETURN_DOUBLE


RETURN_BOOL

RETURN_NULL

留意RETURN_STRING有两个参数
当你需求复制一份字符串时运用
RETURN_STRING("Hello World", 1);


不然运用
RETURN_STRING(str, 0);

这里触及到了模块中内存的分派,当你请求的内存需求php顺序中去释放的话,请参照如下表
Traditional Non-Persistent Persistent
malloc(count)

calloc(count, num) emalloc(count)
ecalloc(count, num) pemalloc(count, 1)*

pecalloc(count, num, 1)
strdup(str)
strndup(str, len) estrdup(str)
estrndup(str, len) pestrdup(str, 1)
pemalloc() & memcpy()
free(ptr) efree(ptr) pefree(ptr, 1)
realloc(ptr, newsize) erealloc(ptr, newsize) perealloc(ptr, newsize, 1)
malloc(count * num + extr)** safe_emalloc(count, num, extr) safe_pemalloc(count, num, extr)
普通我们运用Non-Persistent中列出的这些好了。


 

发布:2007-03-31 14:49    编辑:泛普软件 · xiaona    [打印此页]    [关闭]
南昌OA系统
联系方式

成都公司:成都市成华区建设南路160号1层9号

重庆公司:重庆市江北区红旗河沟华创商务大厦18楼

咨询:400-8352-114

加微信,免费获取试用系统

QQ在线咨询

泛普南昌网站建设公司其他应用

南昌OA软件 南昌OA新闻动态 南昌OA信息化 南昌OA快博 南昌OA行业资讯 南昌软件开发公司 南昌门禁系统 南昌物业管理软件 南昌仓库管理软件 南昌餐饮管理软件 南昌网站建设公司