PHP的HashTable(二)
HashTable的使用示例:
/* Demo */
PHP_METHOD(tonic_application, showglobals)
{
zval *val = NULL;
zval *ht_arr = NULL;
MAKE_STD_ZVAL(ht_arr);
array_init(ht_arr);
MAKE_STD_ZVAL(val);
ZVAL_STRING(val,"dopool",1);
if(zend_hash_add(Z_ARRVAL_P(ht_arr),"a",sizeof("a"),&val,sizeof(zval*), NULL) == FAILURE)
{
php_printf("zend_hash_add errored!\n");
return; /* 直接return会导致内存泄露,所以这里是错误的..是禁止的... */
}
zval **ppzval=NULL;
if( zend_hash_find(Z_ARRVAL_P(ht_arr),"a",sizeof("a"),(void**)&ppzval) == FAILURE)
{
php_printf("error!\n");
}
if( ppzval != NULL)
{
RETVAL_ZVAL(*ppzval, 1, 0);
}
zval_ptr_dtor(&ht_arr);
zval_ptr_dtor(&val);
PHP_METHOD(tonic_application, showglobals)
{
zval *val = NULL;
zval *ht_arr = NULL;
MAKE_STD_ZVAL(ht_arr);
array_init(ht_arr);
MAKE_STD_ZVAL(val);
ZVAL_STRING(val,"dopool",1);
if(zend_hash_add(Z_ARRVAL_P(ht_arr),"a",sizeof("a"),&val,sizeof(zval*), NULL) == FAILURE)
{
php_printf("zend_hash_add errored!\n");
return; /* 直接return会导致内存泄露,所以这里是错误的..是禁止的... */
}
zval **ppzval=NULL;
if( zend_hash_find(Z_ARRVAL_P(ht_arr),"a",sizeof("a"),(void**)&ppzval) == FAILURE)
{
php_printf("error!\n");
}
if( ppzval != NULL)
{
RETVAL_ZVAL(*ppzval, 1, 0);
}
zval_ptr_dtor(&ht_arr);
zval_ptr_dtor(&val);
}
demo是写出来了,可是还没有完全理解HashTable的东东,还要看看...