RE: [PHP] Arrays within classes
| From: | Rick Emery | Date: | Fri, 05 Apr 2002 19:27:42 +0000 |
| Subject: | RE: [PHP] Arrays within classes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-91786@lists.php.net to get a copy of this message | ||
I corrected the syntax errors Erik found and added var_dump() after each $t
equation/assignment
The problem is that you refer to $test->words. You should refer to
$test->words
BIG DIFFERENCE
$test->words means find the value of $words and look for that variable in
$test. That is, if $words ="abc",
then $test->$words means $test->abc
Yes, it's in the manual
-----Original Message-----
From: Brian McLaughlin [mailto:bmclaugh@georgefox.edu]
Sent: Friday, April 05, 2002 12:21 PM
To: php-general@lists.php.net
Subject: [PHP] Arrays within classes
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php