zoukankan      html  css  js  c++  java
  • php 扩展开发

    Linux下用C开发PHP扩展

    一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13

    #> cd /software/php-5.2.13/ext

    二、假设我们要开发一个名为caleng_module的扩展,该扩展包含两个函数:a--处理两个整型相加和b-处理字符串重复输出;

    1、首先编写一个函数定义文件,该文件编写函数原型后缀为def,假设为:caleng_module.def

    1. int a(int x, int y)  
    2. string b(string str, int n)  

     

    2、通过扩展骨架生成器,将在ext目录下自动建立扩展目录caleng_module

    #> ./ext_skel --extname=caleng_module --proto=caleng_module.def

    3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_module/config.m4,将如下行的注释标签"dnl"去掉,修改后如下所示:

    PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,

    Make sure that the comment is aligned:

    [  --enable-myfunctions           Enable myfunctions support])

    4、完善函数a和b的功能: #> vim /software/php-5.2.13/ext/caleng_module/caleng_module.c

    1. PHP_FUNCTION(a)  
    2. {  
    3.     int x, y, z;  
    4.       
    5.     int argc = ZEND_NUM_ARGS();  
    6.    
    7.     if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)  
    8.         return;  
    9.     z = x + y;  
    10.     RETURN_LONG(z);  
    11. }  
    12. PHP_FUNCTION(b)  
    13. {  
    14.     char *str = NULL;  
    15.     int argc = ZEND_NUM_ARGS();  
    16.     int str_len;  
    17.     long n;  
    18.     char *result;  
    19.     char *ptr;  
    20.     int result_length;  
    21.    
    22.     if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)  
    23.         return;  
    24.     result_length = str_len * n;  
    25.     result = (char *) emalloc(result_length + 1);  
    26.     ptr = result;  
    27.     while (n--) {  
    28.         memcpy(ptr, str, str_len);  
    29.         ptr += str_len;  
    30.     }  
    31.     *ptr = '/0';  
    32.     RETURN_STRINGL(result, result_length, 0);  
    33. }  

     

    三、编译安装,假设php的安装目录为:/usr/localhost/webserver/php

    #> cd /software/php-5.2.13/ext/caleng_module

    #> /usr/localhost/webserver/php/bin/phpize 

    #> ./configure --with-php-config=/usr/localhost/webserver/php/bin/php-config

    #> make

    #> make install

    现在将在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目录下生成caleng_module.so文件

    在php.ini配置文件中加入: extension=caleng_module.so.

  • 相关阅读:
    java 判断返回值的类型
    使用反射报异常:object is not an instance of declaring class解决方案
    InvocationTargetException 异常
    在MyEclipse下统计工程的代码(package、行数、类个数)
    跨域问题,及解决方案
    封装原生Ajax发送请求
    win10系统,打不开个性化,并且报错找不到指定模块
    jquery删除内容是动态修改序号
    使用jquery实现返回顶部按钮
    jquery监听video标签视频播放暂停状态
  • 原文地址:https://www.cnblogs.com/breg/p/3544792.html
Copyright © 2011-2022 走看看