Re: Arrays within classes

From: Date: Fri, 05 Apr 2002 19:15:58 +0000
Subject: Re: Arrays within classes
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-91785@lists.php.net to get a copy of this message
I might be completely mistaken here, but it looks like there are a few errors in your code: - No starting brace for the test() method - semi-colon used after class def end-brace - no parentheses after "new test" instance assignment Don't you want to keep your class as general as possible, and perform your error-testing functions on the instances? It's hard for me to tell what kind of data you are trying to represent with this class. My advice is that, even if it seems unnecessarily didactic, you use some sort of actual object to help you visualize what you are trying to do with this class, like Person() or something -- it helps. I can't figure out what this class is even supposed to do. Erik On Friday, April 5, 2002, at 01:21 PM, Brian McLaughlin wrote:
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
---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com

« previous php.general (#91785) next »