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

    基础入门实例

    1. 生成扩展框架

       cd ~/php-7.0.30/ext #进入源码包扩展目录
       ./ext_skel --extname=my_func #生成扩展基本架构
      
    2. 修改配置文件

    • 打开配置文件 config.m4

       dnl Otherwise use enable:
       dnl PHP_ARG_ENABLE(my_func, whether to enable my_func support,
       dnl Make sure that the comment is aligned:
       dnl [  --enable-my_func           Enable my_func support])
       #修改为:
       dnl Otherwise use enable:
       PHP_ARG_ENABLE(my_func, whether to enable my_func support,
       Make sure that the comment is aligned:
       [  --enable-my_func           Enable my_func support])
      

      dnl 是注释符,表示当前行是注释。这段话是说如果此扩展依赖其他扩展,去掉PHP_ARG_WITH段的注释符;否则去掉PHP_ARG_ENABLE段的注释符。显然我们不依赖其他扩展或lib库,所以去掉PHP_ARG_ENABLE段的注释符:

    1. 在 my_func.c 上实现函数功能

       PHP_FUNCTION(my_func)
       {
               //zend_string *strg;
               //strg = strpprintf(0, "hello world.");
               //RETURN_STR(strg);
               char *arg = NULL;
               size_t arg_len;
               char *strg;
               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
                       return;
               }
               strg = strpprintf(0, "Hello, my_func_print: %s", arg);
               RETURN_STR(strg);
       }
      
    2. 添加到编译列表里(my_func.c):

       const zend_function_entry my_func_functions[] = {
               PHP_FE(my_func, NULL)  /*添加这行*/
               PHP_FE(confirm_my_func_compiled,      NULL)           /* For testing, remove later. */
               PHP_FE_END      /* Must be the last line in my_func_functions[] */
       };
      
    3. 编译与安装

       /usr/local/php7/bin/phpize
       ./configure --with-php-config=/usr/local/php-7.0.31/bin/php-config
       make && make install
       #vim usr/local/php7/etc/php.ini
       extension=my_func.so
      
    4. 测试

       php -r "echo my_func('this is a function extension.');"
      

    不使用工具写扩展

    1. 一个扩展(如:world)至少包含3个文件:

      • config.m4(phpize用来准备编译扩展的配置文件)
      • php_world.h(引用包含的头文件)
      • world.c(源码文件)
    2. config.m4

       PHP_ARG_ENABLE(world, whether to enable world support,
       Make sure that the comment is aligned:
       [  --enable-world           Enable hello support])
      
       if test "$PHP_WORLD" != "no"; then
           AC_DEFINE(HAVE_WORLD,1,[ ])
           PHP_NEW_EXTENSION(world, world.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
       fi
      
    3. php_world.h

       #ifndef PHP_WORLD_H
       #define PHP_WORLD_H
      
       extern zend_module_entry hello_module_entry;
       #define phpext_hello_ptr &hello_module_entry
       #define PHP_WORLD_VERSION "0.1.0"
       #define PHP_WORLD_EXTNAME "world"
      
       #endif
      
    4. world.c

       #ifdef HAVE_CONFIG_H
       #include "config.h"
       #endif
      
       #include "php.h"
       #include "php_world.h"
      
       PHP_FUNCTION(world)
       {
           zend_string *strg;
           strg = strpprintf(0, "hello world. (from world module)");
           RETURN_STR(strg);
       }
      
       const zend_function_entry world_functions[] = {
           PHP_FE(world, NULL)
           PHP_FE_END
       };
       zend_module_entry world_module_entry = {
           STANDARD_MODULE_HEADER,
           PHP_WORLD_EXTNAME,
           world_functions,
           NULL,
           NULL,
           NULL,
           NULL,
           NULL,
           PHP_WORLD_VERSION,
           STANDARD_MODULE_PROPERTIES
       };
      
       #ifdef COMPILE_DL_WORLD
       #ifdef ZTS
       ZEND_TSRMLS_CACHE_DEFINE()
       #endif
       ZEND_GET_MODULE(world)
       #endif
      
    5. 编译安装:
      同上

  • 相关阅读:
    一道面试题:按照其描述要求用java语言实现快速排序
    Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析
    关于oracle存储过程的一些知识点
    多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
    Chapter 5: Container
    统计文件夹下java代码行数的小程序--主要是学习任务队列的思想
    利用strut2标签自动生成form前端验证代码
    简单实现web单点登录
    Chapter 4: Tomcat Default Connector
    [python] 格式化方法 format
  • 原文地址:https://www.cnblogs.com/one-villager/p/10338986.html
Copyright © 2011-2022 走看看