php reference behavior
| From: | 22 manopohuji | Date: | Tue, 17 Dec 2002 18:03:42 +0000 |
| Subject: | php reference behavior | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-128584@lists.php.net to get a copy of this message | ||
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!
xomina