Arrays within classes
| From: | Brian McLaughlin | Date: | Fri, 05 Apr 2002 18:21:22 +0000 |
| Subject: | Arrays within classes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-91782@lists.php.net to get a copy of this message | ||
This is driving me crazy! I've created a class to hold data so I can just
put the object into the session rather than saving off each piece of data
separately. But I'm getting odd results from the arrays in my class that I
can't explain. Here's a hunk of code that demonstrates:
<?php
class test {
var $words;
function test()
$t = $this->$words;
echo "1: words = $t<br>\n"; // Shows that $words is empty
$this->$words = array();
$t = $this->$words;
echo "2: words = $t<br>\n"; // Shows that $words is an array
$t = $this->$words["Amy"];
echo "3: words[Amy] = $t<br>\n"; // Shows that $words["Amy"] is also
an
array -- WHAT??
}
};
####### Declare a$ to be an array
echo "1: a = $a<br>\n"; // Shows that $a is empty
$a = array();
$t = $a;
echo "2: a = $t<br>\n"; // Shows that $a is an array
$t = $a["Amy"];
echo "3: a[Amy] = $t<br>\n"; // Shows that $a["Amy"] is also
empty -- GOOD
echo "<br><br>";
###### Instantiate a test object
$test = new test;
?>
When I declare a member variable ($words) within the class and then assign
to it an empty array, it seems all the elements of that array are also
arrays -- they should be empty. When I do the same thing outside of a class
($a), I get the results I expect.
Can anyone tell me why that is?
Thanks
Brian
bmclaugh@georgefox.edu