PHP扩展开发(写一个加法的例子)

411阅读 0评论2012-12-12 phpstar
分类:

参考
首先是config.m4的内容

点击(此处)折叠或打开

  1. dnl
  2. dnl $Id: config.m4 72968 2002-03-12 16:44:00Z sas $
  3. dnl
  4. PHP_ARG_ENABLE(demo, [Whether to enable the "demo" extension], [ enable-demo Enable "demo" extension support])
  5. if test $PHP_DEMO != "no"; then
  6. PHP_SUBST(DEMO_SHARED_LIBADD)
  7. PHP_NEW_EXTENSION(demo, demo.c, $ext_shared)
  8. fi

php_walu.h写的不够完善,如此可能更好

点击(此处)折叠或打开

  1. //php_demo.h
  2. #ifndef DEMO_H
  3. #define DEMO_H

  4. //加载config.h,如果配置了的话
  5. #ifdef HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif

  8. //加载php头文件
  9. #include "php.h"
  10. #define phpext_demo_ptr &demo_module_entry
  11. extern zend_module_entry demo_module_entry;

  12. #endif
下面是加法的代码

点击(此处)折叠或打开

  1. //demo.c
  2. #include "php_demo.h"
  3. //module entry

  4. ZEND_FUNCTION(demo_hello)
  5. {
  6.     php_printf("Hello World!\n");
  7. }

  8. ZEND_FUNCTION(demo_add)
  9. {
  10.     long a;
  11.     long b;
  12.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"ll",&a,&b) == FAILURE)
  13.     {
  14.         RETURN_NULL();
  15.     }

  16.     long c = a + b;

  17.     RETVAL_LONG(c);
  18. }


  19. static zend_function_entry demo_functions[] = {
  20.     ZEND_FE(demo_hello,NULL)
  21.     PHP_FE(demo_add,NULL)
  22.     {NULL,NULL,NULL}
  23. };

  24. zend_module_entry demo_module_entry = {
  25. #if ZEND_MODULE_API_NO >= 20010901
  26.     STANDARD_MODULE_HEADER,
  27. #endif
  28.     "demo", //这个地方是扩展名称,往往我们会在这个地方使用一个宏。
  29.     demo_functions, /* Functions */
  30.     NULL, /* MINIT */
  31.     NULL, /* MSHUTDOWN */
  32.     NULL, /* RINIT */
  33.     NULL, /* RSHUTDOWN */
  34.     NULL, /* MINFO */
  35. #if ZEND_MODULE_API_NO >= 20010901
  36.     "2.1", //这个地方是我们扩展的版本
  37. #endif
  38.     STANDARD_MODULE_PROPERTIES
  39. };

  40. #ifdef COMPILE_DL_DEMO
  41. ZEND_GET_MODULE(demo)
  42. #endif

上一篇:没有了
下一篇:执行脚本使用sh与不使用的区别