包装C++;PHP扩展中的类 我在PHP扩展中遵循for包装C++类。 我正在使用wamp服务器和PHP5.4.16
包装C++;PHP扩展中的类 我在PHP扩展中遵循for包装C++类。 我正在使用wamp服务器和PHP5.4.16,c++,compilation,php-extension,C++,Compilation,Php Extension,PHP\u方法(Car,\uu构造)中出现错误 看来打电话给 car_object *obj = ( car_object *) zend_object_store_get_object ( object TSRMLS_CC ); 那么obj->car不是有效地址。当注释行obj->car=car时,wamp没有关闭。所以我猜obj->car不是有效或合法的地址 下面是我的代码: vehicles.cc: #include "php.h" #include "ca
PHP\u方法(Car,\uu构造)
中出现错误
看来打电话给
car_object *obj = ( car_object *) zend_object_store_get_object ( object TSRMLS_CC );
那么obj->car
不是有效地址。当注释行obj->car=car
时,wamp没有关闭。所以我猜obj->car
不是有效或合法的地址
下面是我的代码:
vehicles.cc:
#include "php.h"
#include "car.h"
#define PHP_VEHICLES_EXTNAME "vehicles"
#define PHP_VEHICLES_EXTVER "0.1"
zend_object_handlers car_object_handlers;
struct car_object {
zend_object std;
Car *car;
};
zend_class_entry *car_ce;
void car_free_storage(void *object TSRMLS_DC)
{
car_object *obj = (car_object *)object;
delete obj->car;
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
delete obj->car;
efree(obj);
}
zend_object_value car_create_handler(zend_class_entry *type TSRMLS_DC)
{
zend_object_value retval;
car_object *obj = (car_object *)emalloc(sizeof(car_object));
memset(obj, 0, sizeof(car_object));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zval *tmp;
zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
object_properties_init(&(obj->std), type);
#endif
retval.handle = zend_objects_store_put(obj, NULL, car_free_storage, NULL TSRMLS_CC);
retval.handlers = &car_object_handlers;
return retval;
}
PHP_METHOD(Car, __construct)
{
long maxGear;
Car *car = NULL;
zval *object = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxGear) == FAILURE) {
RETURN_NULL();
}
car = new Car(maxGear);
car_object *obj = ( car_object *) zend_object_store_get_object(object TSRMLS_CC );
//obj->car = car;
car.h car.cc与教程中的相同
我想我错过了什么。可能函数car\u create\u handler
中对“object\u properties\u init(&(obj->std),type);
”的调用不正确
感谢您的帮助/在您的“无车”功能存储器中,您似乎要调用delete obj->car两次。这可能不是个好主意 此外,当我最近查看另一个php模块时,我发现obj->std.properties并不总是设置为有效地址。因此,在将其传递给zend_hash_destroy之前,可能需要检查它是否为0。我的代码是:
if(obj->std.properties){
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
}
if(obj->std.properties){
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
}