天天看點

PHP擴充參數定義結構和操作詳解

結構體

/*PHP擴充中函數參數結構*/
typedef struct _zend_arg_info {
	const char *name;		//參數名
	zend_uint name_len;		//參數名長度
	const char *class_name;		//類名
	zend_uint class_name_len;	//類名長度
	zend_bool array_type_hint;
	zend_bool allow_null;
	zend_bool pass_by_reference;
	zend_bool return_reference;
	int required_num_args;
} zend_arg_info;
           

操作這個結構的宏:

#define ZEND_ARG_INFO(pass_by_ref, name)			\
	{ #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },
#define ZEND_BEGIN_ARG_INFO(name, pass_rest_by_reference)	\
	ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, ZEND_RETURN_VALUE, -1)
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args)	\
	static const zend_arg_info name[] = {																		\
		{ NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },
#define ZEND_END_ARG_INFO()		};
           

随便找一段參數定義代碼:

ZEND_BEGIN_ARG_INFO_EX(arginfo_dir_openFile, 0, 0, 0)
    ZEND_ARG_INFO(0, open_mode)
    ZEND_ARG_INFO(0, use_include_path)
    ZEND_ARG_INFO(0, context)
ZEND_END_ARG_INFO();
           

那麼這段代碼,經過上面宏替換之後就變成了,注意,在宏裡面#表示把後面的代碼字元串化:

static const zend_arg_info arginfo_dir_openFile[]={
	{NULL, 0, NULL, 0, 0, 0, 0, 0, 0),
	{"open_mode", sizeof("openmode")-1, NULL, 0, 0, 0, 0, 0, 0},
        {"use_include_path", sizeof("use_include_path")-1, NULL, 0, 0, 0, 0, 0, 0},
	{"context", sizeof("context")-1, NULL, 0, 0, 0, 0, 0, 0}
};
           

好了,現在寫段C代碼來測試下吧。一下代碼測試環境WIN7 64bit professional +VS2010 ultimate:

#include <iostream>
using namespace std;

//參數定義結構
#define ZEND_ARG_INFO(pass_by_ref, name)			\
{ #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },

//參數結構
typedef struct _zend_arg_info {
	const char *name;		//參數名
	unsigned int name_len;		//參數名長度
	const char *class_name;		//類名
	unsigned int class_name_len;	//類名長度
	bool array_type_hint;
	bool allow_null;
	bool pass_by_reference;
	bool return_reference;
	int required_num_args;
} zend_arg_info;

int main()
{
	static const zend_arg_info arginfo_dir_openFile[]={
		{NULL, 0, NULL, 0, 0, 0, 0, 0, 0},
		ZEND_ARG_INFO(0, open_mode)
		ZEND_ARG_INFO(0, use_include_path)
		ZEND_ARG_INFO(0, context)
		};
	_asm int 3; //斷點
	return 1;
}
           

在中斷處截圖看一下,發現:

PHP擴充參數定義結構和操作詳解

看到了吧,字元串和字元串長度都有的。

遺忘的C語言細節:

1.#define XXXXX(a,) #a 即表示 把a表現為字元串。   XXXXXX(shyandsy)  ==== "shyandsy"

2.sizeof()可以計算字元串長度。結果是字元個數+1,因為有\0結尾。

sizeof("shyandsy")-1 = 8

PHP擴充參數定義結構和操作詳解