第二章:php7擴展開發[2] 怎麼樣建立類

1.建立擴展
進入${php-src}/ext目錄,執行./ext_skel--extname=route,這時目錄下會出現一個route的目錄,cd ./route

2.修改config.m4內容

第十行左右,去掉dnl

PHP_ARG_WITH(route, for route support,
dnl Make sure that the comment is aligned:
[ --with-route Include route support])

在下面追加到如下內容:

if test -z "$PHP_DEBUG" ; then 
AC_ARG_ENABLE(debug, [--enable-debug compile with debugging system], [PHP_DEBUG=$enableval],[PHP_DEBUG=no] ) 
fi

3.修改php_route.h頭文件內容

在第五十行左右,加入如下內容

//定義類
extern zend_class_entry *route_ce;
//定義loader類中的方法
PHP_METHOD(route_ce,__construct);
PHP_METHOD(route_ce,run);

4.修改route.c文件內容

/* True global resources - no need for thread safety here */
在這行註釋下添加變量聲明

zend_class_entry *route_ce;

找到route_functions,去掉confirm_route_compiled的內容,改成以下

const zend_function_entry route_functions[] = {
//這裏去掉了測試的方法,記得去掉上面的函數實現。處女座有潔癖。
//註冊route類中的方法
ZEND_ME(route, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
ZEND_ME(route,run,NULL,ZEND_ACC_PUBLIC)

PHP_FE_END /* Must be the last line in route_functions[] */
};

注:找到PHP_MINIT_FUNCTION方法,移到route_functions下面來

PHP_MINIT_FUNCTION(route)
{

//註冊route類
zend_class_entry ce;

//define INIT_NS_CLASS_ENTRY(class_container, ns, class_name, functions)
INIT_NS_CLASS_ENTRY(ce,"Dora" ,"Route", route_functions);
route_ce = zend_register_internal_class(&ce TSRMLS_CC);

return SUCCESS;
}

/* Every user-visible function in PHP should document itself in the source */
在這行註釋下添加函數

/**
* 聲明構造函數
* @param
* @return
*/
ZEND_METHOD(route,__construct){

zend_printf("__construct\n");

}


/**
* 加載run
* @param
* @return
*/
ZEND_METHOD(route,run){

zend_printf("route_run\n");

}


//加載模塊
zend_module_entry route_module_entry = {
STANDARD_MODULE_HEADER,

"route",
route_functions,

PHP_MINIT(route),
PHP_MSHUTDOWN(route),
PHP_RINIT(route), 
PHP_RSHUTDOWN(route),

PHP_MINFO(route),
PHP_ROUTE_VERSION,
STANDARD_MODULE_PROPERTIES
};


5.編譯安裝
phpize
./configure --with-php-config=/usr/bin/php-config
make && make install


6.php7建立類全部到的知識點

1.zend_class_entry是內核中定義的一個類的結構體,是內核實現PHP中類與對象的一個結構類型,起到類模板的做用。聲明在Zend/zend.h中

struct _zend_class_entry {
char type;
zend_string *name;
struct _zend_class_entry *parent; //指向父類的指針
int refcount;
uint32_t ce_flags;

int default_properties_count;//默認數據成員個數
int default_static_members_count;//默認類中靜態成員個數
zval *default_properties_table;//默認數據成員變量
zval *default_static_members_table;//默認類中靜態數據成員變量

zval *static_members_table;//類中靜態成員
HashTable function_table; //函數方法表
HashTable properties_info;//數據成員表
HashTable constants_table;//類數據常量

//php中的構造函數、析構函數和魔法函數
union _zend_function *constructor;
union _zend_function *destructor;
union _zend_function *clone;
union _zend_function *__get;
union _zend_function *__set;
union _zend_function *__unset;
union _zend_function *__isset;
union _zend_function *__call;
union _zend_function *__callstatic;
union _zend_function *__tostring;
union _zend_function *__debugInfo;
union _zend_function *serialize_func;
union _zend_function *unserialize_func;

zend_class_iterator_funcs iterator_funcs;

/* 類句柄 */
zend_object* (*create_object)(zend_class_entry *class_type);
zend_object_iterator *(*get_iterator)(zend_class_entry *ce, zval *object, int by_ref);
/* 類聲明的接口 */
int (*interface_gets_implemented)(zend_class_entry *iface, zend_class_entry *class_type); /* a class implements this interface */
union _zend_function *(*get_static_method)(zend_class_entry *ce, zend_string* method);

/* 序列化回調函數指針 */
int (*serialize)(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data);
int (*unserialize)(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data);

uint32_t num_interfaces; // 類實現的接口數
zend_class_entry **interfaces;//類實現的接口變量

uint32_t num_traits;
zend_class_entry **traits;
zend_trait_alias **trait_aliases;
zend_trait_precedence **trait_precedences;

union {
struct {
zend_string *filename;
uint32_t line_start;
uint32_t line_end;
zend_string *doc_comment;
} user;
struct {
const struct _zend_function_entry *builtin_functions;
struct _zend_module_entry *module;
} internal;
} info;
};

2.zend_function_entry是內核中定義的一個函數的結構體,全部類的方法的聲明在zend_function_entry中註冊,聲明在Zend/zend_API.h中。

typedef struct _zend_function_entry {
const char *fname;
void (*handler)(INTERNAL_FUNCTION_PARAMETERS);//函數句柄
const struct _zend_internal_arg_info *arg_info;//函數所需參數
uint32_t num_args;//參數個數
uint32_t flags;
} zend_function_entry;


聲明在Zend/zend_API.h中
define ZEND_ME(classname, name, arg_info, flags) 
ZEND_FENTRY(name, ZEND_MN(classname##_##name), arg_info, flags)

define ZEND_FENTRY(zend_name, name, arg_info, flags) 
{ #zend_name, name, arg_info, (uint32_t) (sizeof(arg_info)/sizeof(struct _zend_internal_arg_info)-1), flags },

define PHP_FE_END ZEND_FE_END
define ZEND_FE_END { NULL, NULL, NULL, 0, 0 }


3.zend_module_entry是內核中定義的一個擴展模塊的結構體,註冊擴展模塊到PHP運行過程當中,聲明在Zend/zend_modules.h中。

struct _zend_module_entry {
unsigned short size;
unsigned int zend_api;
unsigned char zend_debug;
unsigned char zts;

const struct _zend_ini_entry *ini_entry;//指向php.ini的指針
const struct _zend_module_dep *deps; //擴展依賴,deps就是用來註冊依賴、衝突模塊的

const char *name; //擴展的名稱
const struct _zend_function_entry *functions;//指向zend_functions_entry指針

int (*module_startup_func)(INIT_FUNC_ARGS);//模塊初始化時被調用的函數指針
int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);//模塊被關閉時調用的函數指針
int (*request_startup_func)(INIT_FUNC_ARGS);//每處理一次請求前調用此函數
int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);//每處理一次請求先後調用此函數


void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS); //phpinfo時打印出的關於此擴展的信息
const char *version;//擴展的版本號

size_t globals_size;
#ifdef ZTS
ts_rsrc_id* globals_id_ptr;
#else
void* globals_ptr;
#endif
void (*globals_ctor)(void *global);
void (*globals_dtor)(void *global);
int (*post_deactivate_func)(void);
int module_started;
unsigned char type;
void *handle;
int module_number;
const char *build_id;
};


struct _zend_module_dep {
const char *name; /* module name */
const char *rel; /* version relationship: NULL (exists), lt|le|eq|ge|gt (to given version) */
const char *version; /* version */
unsigned char type; /* dependency type */
};

若是有依賴,在php擴展中,須要這個寫好依賴。或者php.ini中的擴展按從上到下的順序加載你也能夠先寫pdo後面追加你的擴展模塊名。
static const zend_module_dep demo_deps[] = {
ZEND_MOD_REQUIRED("pdo")
ZEND_MOD_END
};
  • 請尊重本人勞動成功,能夠隨意轉載但保留如下信息
  • 做者:歲月經年
  • 時間:2016年03月
  • 首發:http://www.djhull.com
相關文章
相關標籤/搜索