Re: php reference behavior
| From: | Peter Clarke | Date: | Tue, 17 Dec 2002 18:15:55 +0000 |
| Subject: | Re: php reference behavior | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-128593@lists.php.net to get a copy of this message | ||
22 Manopohuji wrote:
hi, i'm running into some weird behavior with php references. i distilled it down to some test code below: <?php $array = array( 'name' => 'dean' ); $var1['arrayref'] = & $array; $var2 = $var1; echo "var1:\n"; print_r( $var1 ); echo "var2:\n"; print_r( $var2 ); $var1['arrayref'] = NULL; echo "var1:\n"; print_r( $var1 ); echo "var2:\n"; print_r( $var2 ); ?> it seems that setting a hash key to a reference to something, and then repointing that key to NULL, apparently sets the original 'something' to NULL instead of just 'repointing' the hash key! am i missing something obvious here, or is this behavior not what you'd normally expect? i wrote similar code in perl, and it behaved as i expected: the second hash still pointed to the original target (i am almost certain c/c++ and java also behave this way). so what is going on with php? does anyone know how to get it to do what i want it to do -- i.e., merely unset the key mapping of one of the hashes, leaving the other hash still pointing at the target? thansk for any insight you might be able to give! xominaHave a look at: http://www.php.net/manual/en/language.references.php <quote>They are not like C pointers, they are symbol table aliases.</quote> Instead of: $var1['arrayref'] = NULL; use: unset($var1['arrayref']); Peter