• 用ext_skel,实现一个PHP扩展,添加到PHP并调用


    1 创建函数定义文件

    #mkdir /home/phpext
    #vi mydefined.skel
    
    string get_text(string str)

    根据README所提供的信息创建预定义文件和扩展的开发框架包

    进入到PHP源码包,即php-5.*/ext/内 运行下面代码 将会生成hello文件夹

    # ./ext_skel --extname=hello --proto=/home/phpext/mydefined.skel

    3 进入hello文件夹 修改hello文件内,config.m4、php_hello.h、hello.c三个文件

    [root@localhost hello]#vi config.m4

    dnl PHP_ARG_WITH(hello, for hello support,
    dnl Make sure that the comment is aligned:
    dnl [  --with-hello             Include hello support])
    
    dnl Otherwise use enable:
    
    PHP_ARG_ENABLE(hello, whether to enable hello support,
    Make sure that the comment is aligned:
    [  --enable-hello           Enable hello support])

    注释掉PHP_ARG_WITH或PHP_ARG_ENABLE(根据实际情况二选一,第一种是指扩展需第三方库支持,我这去掉第二个的注释dnl)

    [root@localhost hello]#vi php_hello.h

     /*PHP_FUNCTION(confirm_hello_compiled);*/   /* For testing, remove later. */
     PHP_FUNCTION(get_text);

    注释掉默认声明的PHP函数 confirm_hello_compiled

    [root@localhost hello]# vi hello.c

    const zend_function_entry hello_functions[] = {
    /*  PHP_FE(confirm_hello_compiled,  NULL)*/     /* For testing, remove later. */
        PHP_FE(get_text,    NULL)
        PHP_FE_END  /* Must be the last line in hello_functions[] */
    };
    
    
    /*PHP_FUNCTION(confirm_hello_compiled)*/
    PHP_FUNCTION(get_text)
    {
        char *arg = NULL;
        int arg_len, len;
        char *strg;
    
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
            return;
        }
    
        len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "hello", arg);
        RETURN_STRINGL(strg, len, 0);
    }

    注释掉默认声明的PHP函数  confirm_hello_compiled 并鸠占鹊巢 用get_text 代替confirm_hello_compiled (主要使用里面现成的打印内容;可以自己写要执行的内容实现getext函数 需熟悉C)

    zend_parse_parameters
    第一个参数是传递给函数的参数个数。通常的做法是传给它ZEND_NUM_ARGS()。这是一个表示传递给函数参数总个数的宏。第二个参数是为了线程安全,总是传递TSRMLS_CC宏,后面会讲到。第三个参数是一个字符串,指定了函数期望的参数类型,后面紧跟着需要随参数值更新的变量列表。因为PHP采用松散的变量定义和动态的类型判断,这样做就使得把不同类型的参数转化为期望的类型成为可能。例如,如果用户传递一个整数变量,可函数需要一个浮点数,那么zend_parse_parameters()就会自动地把整数转换为相应的浮点数。如果实际值无法转换成期望类型(比如整形到数组形),会触发一个警告。

    配置、编译、安装phpext

    [root@localhost hello]# /usr/local/php/bin/phpize
    [root@localhost hello]#./configure --with-php-config=/usr/local/php/bin/php-config
    [root@localhost hello]#make && make install

    如果一切正常,将在对应的文件夹内将多出一个叫hello.so的文件,具体地址编译后会返回 在php.ini中添加扩展并重启 如

    extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/hello.so

    查看是否安装成功

    #php -m|grep hello
    hello
    [root@localhost hello]#  php -r "echo get_text('hello');"
    Congratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP.

     盗用别人的一张图

    详细可参考:http://www.laruence.com/2009/04/28/719.html

    下一步学习C 任重道远...

  • 相关阅读:
    nginx+redis 实现 jsp页面缓存,提升系统吞吐率
    mybatis做like模糊查询
    java自定义注解实现前后台参数校验
    sql like 通配符 模糊查询技巧及特殊字符
    Hbuilder 常用快捷键汇总
    史上最全最强SpringMVC详细示例实战教程
    软件设计师&产品经理应常去的网站
    ActionMQ5.8.0 JMS实例 手把手详细图解
    Maven简单使用
    Maven 使用介绍
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4995478.html
Copyright © 2020-2023  润新知