第二章: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
");

}


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

zend_printf("route_run
");

}


//加载模块
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
 

原文地址:https://www.cnblogs.com/djhull/p/5383067.html