Re: [PHP-DEV] Modifying PHP variables in C

[email protected] (Moriyoshi Koizumi)
Newsgroups php.dev
Message-ID <[email protected]>
You should use zval_ptr_dtor() to dispose the old value. Note the old 
value won't actually be freed as long as any reference to the variable is 
alive.

for example,

zval *ary1, *ary2;

/* $ary1 = array(); */
ALLOC_INIT_ZVAL(ary1);
array_init(ary1);

/* $ary2 = array(); */
ALLOC_INIT_ZVAL(ary2);
array_init(ary2);

/* $ary1['key'] = 123; */
add_assoc_long_ex(ary1, "key", sizeof("key"), 123);

/* $ary2['key'] = 456; */
add_assoc_long_ex(ary2, "key", sizeof("key"), 456);

/* $ary1 = $ary2 */
zval_add_ref(ary2);
zval_ptr_dtor(&ary1);
ary1 = ary2;


Moriyoshi

"John Lim" <[email protected]> wrote:

> Hi,
> 
> I'm porting some PHP code to C, and was hoping that someone can help me.
> 
> I have 2 variables $oldarray  and $newarray that both hold arrays and want
> to set
> 
>   $oldarray = $newarray;
> 
> I suppose i have to dispose of $oldarray before i set it to $newarray. I'm
> not sure what is the best way, so i did this.
> 
> zval **oldarray,*newarray;
> 
> zval_add_ref(&newarray);
> convert_to_null_ex(oldarray);
> *oldarray = newarray;
> 
> Would this work? Secondly is there a better way. Thanks again.
> 
> John Lim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.