/*注册STRING类型常量*/
#define REGISTER_STRINGL_CONSTANT(name, str, len, flags) zend_register_stringl_constant((name), sizeof(name), (str), (len), (flags), module_number TSRMLS_CC)
7、全局变量
#php-fpm 生成 POST|GET|COOKIE|SERVER|ENV|REQUEST|FILES全局变量的流程 php_cgi_startup() -> php_module_startup() -> php_startup_auto_globals() -> 保存变量到symbol_table符号表 php_cgi_startup()在 fpm/fpm/fpm_main.c中定义 php_module_startup() 在main/main.c中定义 php_startup_auto_globals() 在main/php_variables.h中定义 zend_hash_update(&EG(symbol_table), "_GET", sizeof("_GET") + 1, &vars, sizeof(zval *), NULL); /* 读取$_SERVER变量 */ static PHP_FUNCTION(print_server_vars) { zval **val; if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **)&val) == SUCCESS) { RETURN_ZVAL(*val, 1, 0); }else{ RETURN_FALSE; } } /* 读取$_SERVER[$name] */ ZEND_BEGIN_ARG_INFO(print_server_var_arginfo, 0) ZEND_ARG_INFO(0, "name") ZEND_END_ARG_INFO() static PHP_FUNCTION(print_server_var) { char *name; int name_len; zval **val; HashTable *ht_vars = NULL; HashPosition pos; zval **ret_val; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &name, &name_len) == FAILURE) { RETURN_NULL(); } if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **)&val) == SUCCESS) { ht_vars = Z_ARRVAL_PP(val); //此处需传入大于name长度+1的值,因为字符串值后面需要'\0' if (zend_hash_find(ht_vars, name, name_len+1, (void **)&ret_val) == SUCCESS) { RETURN_STRING(Z_STRVAL_PP(ret_val), 0); }else{ RETURN_NULL(); } }else{ RETURN_NULL(); } }
8、包装第三方库
配置(config.m4)
SEARCH_PATH="/usr/local /usr" #lib搜索的目录 SEARCH_FOR="/include/curl/curl.h" #lib头文件的路径 if test -r $PHP_LIBS/$SEARCH_FOR; then LIBS_DIR=$PHP_LIBS else # search default path list AC_MSG_CHECKING([for libs files in default path]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then LIBS_DIR=$i #搜索到的lib的路径 AC_MSG_RESULT(found in $i) fi done fi /*验证lib是否存在*/ if test -z "$LIBS_DIR"; then AC_MSG_RESULT([not found]) AC_MSG_ERROR([Please reinstall the libs distribution]) fi /*编译的时候添加lib的include目录, -I/usr/include*/ PHP_ADD_INCLUDE($LIBS_DIR/include) LIBNAME=curl #lib名称 LIBSYMBOL=curl_version #lib的一个函数,用来PHP_CHECK_LIBRARY验证lib /*验证lib*/ PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, [ PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $LIBS_DIR/$PHP_LIBDIR, LIBS_SHARED_LIBADD) #编译的时候链接lib, -llibcurl AC_DEFINE(HAVE_LIBSLIB,1,[ ]) ],[ AC_MSG_ERROR([wrong libs lib version or lib not found]) ],[ -L$LIBS_DIR/$PHP_LIBDIR -lm ]) PHP_SUBST(LIBS_SHARED_LIBADD)
9、用于返回的宏
//这些宏都定义在Zend/zend_API.h文件里
#define RETVAL_RESOURCE(l) ZVAL_RESOURCE(return_value, l)
#define RETVAL_BOOL(b) ZVAL_BOOL(return_value, b)
#define RETVAL_NULL() ZVAL_NULL(return_value)
#define RETVAL_LONG(l) ZVAL_LONG(return_value, l)
#define RETVAL_DOUBLE(d) ZVAL_DOUBLE(return_value, d)
#define RETVAL_STRING(s, duplicate) ZVAL_STRING(return_value, s, duplicate)
#define RETVAL_STRINGL(s, l, duplicate) ZVAL_STRINGL(return_value, s, l, duplicate)
#define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value)
#define RETVAL_ZVAL(zv, copy, dtor) ZVAL_ZVAL(return_value, zv, copy, dtor)
#define RETVAL_FALSE ZVAL_BOOL(return_value, 0)
#define RETVAL_TRUE ZVAL_BOOL(return_value, 1)
#define RETURN_RESOURCE(l) { RETVAL_RESOURCE(l); return; }
#define RETURN_BOOL(b) { RETVAL_BOOL(b); return; }
#define RETURN_NULL() { RETVAL_NULL(); return;}
#define RETURN_LONG(l) { RETVAL_LONG(l); return; }
#define RETURN_DOUBLE(d) { RETVAL_DOUBLE(d); return; }
#define RETURN_STRING(s, duplicate) { RETVAL_STRING(s, duplicate); return; }
#define RETURN_STRINGL(s, l, duplicate) { RETVAL_STRINGL(s, l, duplicate); return; }
#define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; }
#define RETURN_ZVAL(zv, copy, dtor) { RETVAL_ZVAL(zv, copy, dtor); return; }
#define RETURN_FALSE { RETVAL_FALSE; return; }
#define RETURN_TRUE { RETVAL_TRUE; return; }
其实,除了这些标量类型,还有很多php语言中的复合类型我们需要在函数中返回,如数组和对象,我们可以通过RETVAL_ZVAL与RETURN_ZVAL来操作它们
10、hashTable的遍历函数