将PHP C++扩展从php5升级到php7

在没有怎么看明白php5 php7源码的情况下,接手一份基于php5写c++扩展,如何接手快速升级到php7环境下也能使用呢;我仅仅修改了所引用的一个php中对象处理的头文件,就满足了要求,扩展被编译通过,运行正常。


phpobj.h 文件,定义版本选择

#ifndef _PHP_OBJ_H_2017
#define _PHP_OBJ_H_2017
#include <php_version.h>
#if PHP_MAJOR_VERSION < 7
#include "php5obj.h"
#else
#include "php7obj.h"
#endif
#endif//_PHP_OBJ_H_2017

php5obj.h 旧的
#define SW_RETURN_STRINGL    RETURN_STRINGL
#define SW_RETVAL_STRINGL    RETVAL_STRING
#define SW_RETVAL_STRING     RETVAL_STRING
#define SW_RETURN_STRING     RETURN_STRING
#define SW_ZVAL_STRINGL 	ZVAL_STRINGL
#define SW_ZVAL_STRING      ZVAL_STRING
#define SW_MAKE_STD_ZVAL(p) MAKE_STD_ZVAL(p)
#define  PZVAL_IS_REF(len)  ZVAL_IS_REF(len) 


// Register the class entry..
#define PHP5CPP_REGISTER_CLASS(name, obj_name) \
    { \
        zend_class_entry ce; \
        INIT_CLASS_ENTRY(ce, obj_name, php5cpp_ ## name ##_methods); \
        ce.create_object = php5cpp_object_new_ ## name; \
        php5cpp_ce_ ## name = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);  \
        memcpy( &php5cpp_object_handlers_ ## name, \
                zend_get_std_object_handlers(), \
                sizeof(zend_object_handlers) ); \
        php5cpp_object_handlers_ ## name.clone_obj = NULL; \
        php5cpp_ce_ ## name->ce_flags |= ZEND_ACC_FINAL_CLASS; \
    }


#define PHP5CPP_GET_THIS() \
    zval* object = getThis();


// Register resources. If we're using an object, put it into the object store.
#define PHP5CPP_REGISTER_RESOURCE(obj_type, return_value, res, le) \
    { \
        PHP5CPP_GET_THIS(); \
        int rsrc_id = ZEND_REGISTER_RESOURCE(object ? NULL : return_value, res, le); \
        if (object) { \
            php5cpp_obj *obj = (php5cpp_obj *)zend_object_store_get_object(object TSRMLS_CC); \
            obj->u.obj_type  = res; \
            obj->rsrc_id     = rsrc_id; \
            obj->type        = is_ ## obj_type; \
        } \
    }


// These are for parsing parameters and getting the actual object from the store.
#define PHP5CPP_SET_OBJ(type) \
    { \
        php5cpp_obj *obj  = (php5cpp_obj *)zend_object_store_get_object( object TSRMLS_CC ); \
        type ## _instance = obj->u.type; \
    }

// Deprecated
#define PHP5CPP_OBJ_PARAMS(type, params) \
    { \
        PHP5CPP_GET_THIS(); \
        if (object) { \
            if (params == FAILURE) { \
                RETURN_FALSE; \
            } \
            PHP5CPP_SET_OBJ(type) \
        } \
    }

// Deprecated
#define PHP5CPP_OBJ_NO_PARAMS(type) \
    { \
        PHP5CPP_GET_THIS(); \
        if (object) { \
            if (ZEND_NUM_ARGS() != 0) { \
                php_error(E_WARNING, "didn't expect any arguments in %s()", get_active_function_name(TSRMLS_C)); \
            } \
            PHP5CPP_SET_OBJ(type) \
        } \
    }

#define PHP5CPP_GET_OBJ(type) \
    { \
        PHP5CPP_GET_THIS(); \
        if (object) { \
            PHP5CPP_SET_OBJ(type) \
        } \
        if (type ## _instance == NULL) { \
            php_error(E_WARNING, "Get object fall in %s()", get_active_function_name(TSRMLS_C)); \
        } \
    }

#define PHP5CPP_GET_OBJ_ZVAL(type, obj) \
    { \
        zval* object = obj; \
        if (object) { \
            PHP5CPP_SET_OBJ(type) \
        } \
        if (type ## _instance == NULL) { \
            php_error(E_WARNING, "Get object fall in %s()", get_active_function_name(TSRMLS_C)); \
        } \
    }

    //static void php5cpp_object_free_storage_ ## type(zend_object *object TSRMLS_DC) 
#define PHP5CPP_OBJ_FREE_FUNCTION(type) \
    static void php5cpp_object_free_storage_ ## type(void *object TSRMLS_DC) \
    { \
        php5cpp_obj *obj = (php5cpp_obj *) object; \
        zend_hash_destroy(obj->std.properties); \
        FREE_HASHTABLE(obj->std.properties); \
        if ( obj->u.type ) { \
            zend_list_delete(obj->rsrc_id); \
        } \
        efree(obj); \
    }


#define PHP5CPP_OBJ_NEW_FUNCTION(type) \
    static zend_object_value php5cpp_object_new_ ## type(zend_class_entry *class_type TSRMLS_DC) \
    { \
        zend_object_value retval; \
        zval *tmp; \
        php5cpp_obj *obj = (php5cpp_obj *)emalloc( sizeof(php5cpp_obj) ); \
        memset( obj, 0, sizeof(php5cpp_obj) ); \
        obj->std.ce = class_type; \
        ALLOC_HASHTABLE( obj->std.properties ); \
        zend_hash_init( obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0 ); \
        zend_hash_copy( obj->std.properties, \
                        &class_type->properties_info, \
                        (copy_ctor_func_t) zval_add_ref, \
                        (void *) &tmp, \
                        sizeof(zval *) ); \
        retval.handle = zend_objects_store_put( obj, \
                                                NULL, \
                                                (zend_objects_free_object_storage_t)php5cpp_object_free_storage_ ## type, \
                                                NULL TSRMLS_CC ); \
        retval.handlers = &php5cpp_object_handlers_ ## type; \
        return retval; \
    }

#endif



php7obj.h  新的

#define SW_RETURN_STRINGL(s, l, dup)    RETURN_STRINGL(s, l)
#define SW_RETVAL_STRINGL(s, l, dup)    do{RETVAL_STRINGL(s, l); if (dup == 0) efree(s);}while(0)
#define SW_RETVAL_STRING(s, dup)        do{RETVAL_STRING(s); if (dup == 0) efree(s);}while(0)
#define SW_RETURN_STRING(val, duplicate)     RETURN_STRING(val)

#define SW_ZVAL_STRINGL(z, s, l, dup)         ZVAL_STRINGL(z, s, l)
#define SW_ZVAL_STRING(z,s,dup)               ZVAL_STRING(z,s)

#define SW_MAKE_STD_ZVAL(p)             zval _stack_zval_##p; p = &(_stack_zval_##p)
#define  PZVAL_IS_REF(len)              0

static inline php5cpp_obj* php5obj_from_obj(zend_object *obj) {
        return (php5cpp_obj*)((char*)(obj) - XtOffsetOf(php5cpp_obj, std));
}

#define Z_PHP5OBJ_P(zv)  php5obj_from_obj(Z_OBJ_P((zv)))



// Register the class entry..
#define PHP5CPP_REGISTER_CLASS(name, obj_name)\
{\
zend_class_entry ce;\
INIT_CLASS_ENTRY(ce, obj_name, php5cpp_##name##_methods);\
ce.create_object = php5cpp_object_new_##name; \
php5cpp_ce_##name = zend_register_internal_class_ex(&ce, NULL);  \
memcpy( &php5cpp_object_handlers_##name, zend_get_std_object_handlers(),sizeof(zend_object_handlers) );\
php5cpp_object_handlers_##name.clone_obj = NULL;\
}



//#define ZEND_REGISTER_RESOURCE(return_value, result, le_result)  ZVAL_RES(return_value,zend_register_resource(result, le_result))

#define PHP5CPP_GET_THIS() \
	zval* object = getThis();


// Register resources. If we're using an object, put it into the object store.
#define PHP5CPP_REGISTER_RESOURCE(obj_type, return_value, res, le) \
{\
PHP5CPP_GET_THIS();\
int rsrc_id =0;\
/*zend_register_resource(res,le);*/\
if (object) { \
php5cpp_obj *obj = Z_PHP5OBJ_P(object);\
obj->u.obj_type  = res;\
obj->rsrc_id     = rsrc_id;\
obj->type        = is_##obj_type;\
 }\
}


// These are for parsing parameters and getting the actual object from the store.
#define PHP5CPP_SET_OBJ(type_name) \
{ \
php5cpp_obj *obj  = Z_PHP5OBJ_P(object) ; \
type_name ## _instance = obj->u.type_name;\
}

// Deprecated
#define PHP5CPP_OBJ_PARAMS(type, params) \
{ \
	PHP5CPP_GET_THIS(); \
	if (object) { \
	if (params == FAILURE) { \
	RETURN_FALSE; \
	} \
	PHP5CPP_SET_OBJ(type) \
	} \
}

// Deprecated
#define PHP5CPP_OBJ_NO_PARAMS(type) \
{ \
	PHP5CPP_GET_THIS(); \
	if (object) { \
	if (ZEND_NUM_ARGS() != 0) { \
	php_error(E_WARNING, "didn't expect any arguments in %s()", get_active_function_name(TSRMLS_C)); \
	} \
	PHP5CPP_SET_OBJ(type) \
	} \
}

#define PHP5CPP_GET_OBJ(type) \
{\
	PHP5CPP_GET_THIS();\
        PHP5CPP_SET_OBJ(type);\	
}

#define PHP5CPP_GET_OBJ_ZVAL(type, obj) \
{ \
	zval* object = obj; \
	if (object) { \
	PHP5CPP_SET_OBJ(type) \
	} \
	if (type ## _instance == NULL) { \
	php_error(E_WARNING, "Get object_zal fall in %s()", get_active_function_name(TSRMLS_C)); \
	} \
}


#define PHP5CPP_OBJ_FREE_FUNCTION(type) \
	static void php5cpp_object_free_storage_ ## type(void *object TSRMLS_DC) \
{\
php5cpp_obj *obj = Z_PHP5OBJ_P((zval *)object);\
zend_object_std_dtor(&obj->std);\
}



#define PHP5CPP_OBJ_NEW_FUNCTION(type) \
	static zend_object* php5cpp_object_new_ ## type(zend_class_entry *class_type TSRMLS_DC) \
{\
int objsize = sizeof(php5cpp_obj);\
php5cpp_obj *obj=(php5cpp_obj*)ecalloc(1,objsize);memset(obj,0,objsize);\         
php_error(E_WARNING, "create object %p in %s()",obj,get_active_function_name(TSRMLS_C));\
zend_object_std_init(&obj->std, class_type TSRMLS_CC);object_properties_init(&obj->std, class_type);\
obj->std.handlers = &php5cpp_object_handlers_##type;\
return &obj->std;\
}

#endif//_PHP7OBJ_H_







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值