php7扩展编写的时候,提供的一些内核方法和之前的php之前的版本并不能完全兼容。有不少方法参数做了调整。下面是在迁移过程中遇到的一些问题。记录下来,避免大家再踩坑。
php7扩展开发之hello word
白话php7扩展开发之创建对象 add_assoc_stringl
方法参数改为四个。
add_assoc_stringl(parray, key, value, value_len);
迁移的时候,只要把最后一个参数删除即可。
add_assoc_string
方法参数从四个改为了三个。
add_assoc_stringl(parray, key, value);
迁移的时候,只要把最后一个参数删除即可。
add_next_index_stringl
方法参数从四个改为了三个。
add_next_index_stringl(parray, value, value_len);
迁移的时候,只要把最后一个参数删除即可。
add_next_index_string
方法参数从三个改为了两个。
add_next_index_string(parray, value);
迁移的时候,只要把最后一个参数删除即可。
return_stringl
方法参数从三个改为了二个。
return_stringl(value, length);
迁移的时候,只要把最后一个参数删除即可。
错误:‘int64_max’ 在此作用域中尚未声明 原因为深入研究。在 #include php.h 上面加上一行
#include #ifndef int64_max# define int64_max int64_c( 9223372036854775807)#endif#ifndef int64_min# define int64_min (-int64_c( 9223372036854775807)-1)#endif
解决。
创建类 可以参考mysqli扩展mysqli.c文件中的 mysqli_objects_new 方法。
变量声明从堆上分配,改为栈上分配。 如,原先代码为
zval* sarray_l;alloc_init_zval(sarray_l);array_init(sarray_l);
改为
zval sarray_l;array_init(&sarray_l);
zend_hash_get_current_key_ex 方法参数从六个改为了四个。
zend_api int zend_fastcall zend_hash_get_current_key_ex(const hashtable *ht, zend_string **str_index, zend_ulong *num_index, hashposition *pos);
迁移的时候,只要把第三个和第五个参数删除即可。
错误:‘z_type_pp’ 在此作用域中尚未声明 已经没有z_type_pp宏,只有z_type 和z_type_p宏方法的定义。
h2错误:不能从 ‘zend_string*’ 转换到 ‘const char*’
php7中对于字符串的处理使用zend_string进行存储。如果想把zend_string 转为 const char 。需要使用 zstr_val()宏方法。代码如下:
zend_string *str;char *sptr;.....sptr = zstr_val(str);
错误:‘is_bool’ 在此作用域中尚未声明 已经没有is_bool类型。而是分为了is_true 和 is_false.
错误:‘z_bval’ 在此作用域中尚未声明 已经没有z_bval宏。但是可以通过类型是否是 is_true和is_false来判定。如果类型为is_true,则值为true。如果类型为is_false则值为false。
错误:‘zend_object_store_get_object’ 在此作用域中尚未声明 增加如下代码:
static inline hsf_object *hsf_fetch_object(zend_object *obj) /* {{{ */ { return (user_object *)((char*)(obj) - xtoffsetof(user_object, std)); } /* }}} */ #define z_userobj_p(zv) hsf_fetch_object(z_obj_p((zv)))
然后把zend_object_store_get_object改为z_userobj_p即可。注意,user_object是你定义的结构体。
原文链接: php扩展迁移为兼容php7记录,转载请注明来源!