如何将php对象保存到php扩展中的哈希表?
如何将php对象保存到hashtable,以及如何在php扩展中从hashtable获取保存的php对象 这是我的密码:如何将php对象保存到php扩展中的哈希表?,php,php-extension,Php,Php Extension,如何将php对象保存到hashtable,以及如何在php扩展中从hashtable获取保存的php对象 这是我的密码: //object_map has been init and alloc memory //return_value is the return var of PHP_FUNCTION zend_class_entry **class_ce, object_init_ex(return_value, *class_ce); //create object ioc_add_o
//object_map has been init and alloc memory
//return_value is the return var of PHP_FUNCTION
zend_class_entry **class_ce,
object_init_ex(return_value, *class_ce); //create object
ioc_add_object_to_hash(name, return_value ); //save object
//find in the hashtable
if( zend_hash_find( object_map, name, sizeof(name), (void*)&return_value ) == SUCCESS ){
php_printf("return_value:%p, %p,type:%d\n", return_value,&return_value, Z_TYPE_P(return_value));
return 0;
}
//definition of ioc_add_object_to_hash
int ioc_add_object_to_hash( const char *name, zval *obj )
{
if( !obj ){
return -1;
}
if( !object_map ){
return -1;
}
if( zend_hash_update( object_map, name, sizeof(name), obj, sizeof(*obj), NULL) == SUCCESS ){
return 0;
}
return -1;
}
PHP代码:
$filelist = array(
"Foo" => realpath(__DIR__)."/Foo.php",
"Bar" => realpath(__DIR__)."/Bar.php",
);
ioc::init( $filelist );
var_dump(get_included_files());
Bar::halo();
$foo = ioc::make("Foo");
echo $foo->get();
$foo2 = ioc::make("Foo"); var_dump($foo2);
$foo2在第二次调用ioc::make(“Foo”)后为空
我所有的代码都是push-to
谢谢大家! 嘿,我自己解决了!我的目的是从hashtable中获取对象,问题是我用错误的方法初始化返回值,所以我得到了NULL。正确的方法如下所示:
if( !object_map ){
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "object_map had not been alloced");
return -1;
}
zval **obj;
if( zend_hash_find( object_map, name, sizeof(name), (void **)&obj ) == SUCCESS ){
Z_TYPE_P(return_value) = IS_OBJECT;
Z_OBJVAL_P(return_value) = Z_OBJVAL_PP(obj);
//zval_ptr_dtor(obj);
return 0;
}
return -1;
关键是:
Z_TYPE_P(return_value) = IS_OBJECT;
Z_OBJVAL_P(return_value) = Z_OBJVAL_PP(obj);