Re: [PHP] Arrays within classes

2002-04-05 Thread Erik Price

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 = $tbr\n;// Shows that $words is empty
 $this-$words = array();
 $t = $this-$words;
 echo 2: words = $tbr\n;   // Shows that $words is an array
 $t = $this-$words[Amy];
 echo 3: words[Amy] = $tbr\n;  // Shows that $words[Amy] is 
 also an
 array -- WHAT??
   }
 };

 ###  Declare a$ to be an array
 echo 1: a = $abr\n; // Shows that $a is empty
 $a = array();
 $t = $a;
 echo 2: a = $tbr\n; // Shows that $a is an array
 $t = $a[Amy];
 echo 3: a[Amy] = $tbr\n;   // Shows that $a[Amy] is also
 empty -- GOOD

 echo brbr;

 ## 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
 [EMAIL PROTECTED]




 --
 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
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Arrays within classes

2002-04-05 Thread Rick Emery

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:[EMAIL PROTECTED]]
Sent: Friday, April 05, 2002 12:21 PM
To: [EMAIL PROTECTED]
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 = $tbr\n;// Shows that $words is empty
$this-$words = array();
$t = $this-$words;
echo 2: words = $tbr\n;   // Shows that $words is an array
$t = $this-$words[Amy];
echo 3: words[Amy] = $tbr\n;  // Shows that $words[Amy] is also an
array -- WHAT??
  }
};

###  Declare a$ to be an array
echo 1: a = $abr\n; // Shows that $a is empty
$a = array();
$t = $a;
echo 2: a = $tbr\n; // Shows that $a is an array
$t = $a[Amy];
echo 3: a[Amy] = $tbr\n;   // Shows that $a[Amy] is also
empty -- GOOD

echo brbr;

## 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
[EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Arrays within classes

2002-04-05 Thread Rick Emery

Corrected typo:

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

-Original Message-
From: Rick Emery [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 05, 2002 1:28 PM
To: 'Brian McLaughlin'; [EMAIL PROTECTED]
Subject: RE: [PHP] Arrays within classes


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:[EMAIL PROTECTED]]
Sent: Friday, April 05, 2002 12:21 PM
To: [EMAIL PROTECTED]
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 = $tbr\n;// Shows that $words is empty
$this-$words = array();
$t = $this-$words;
echo 2: words = $tbr\n;   // Shows that $words is an array
$t = $this-$words[Amy];
echo 3: words[Amy] = $tbr\n;  // Shows that $words[Amy] is also an
array -- WHAT??
  }
};

###  Declare a$ to be an array
echo 1: a = $abr\n; // Shows that $a is empty
$a = array();
$t = $a;
echo 2: a = $tbr\n; // Shows that $a is an array
$t = $a[Amy];
echo 3: a[Amy] = $tbr\n;   // Shows that $a[Amy] is also
empty -- GOOD

echo brbr;

## 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
[EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within classes

2002-04-05 Thread Brian McLaughlin

Hi Erik

Thanks for the reply.  I'm not sure how I missed the opening { in my
message -- I copy/pasted the code.  But the opening { is definitely there in
the code -- I'd get a nice error message if it weren't.  I believe it's OK
to have a ; after the class def end-brace, but I removed it and I got the
same symptoms.  I also believe it's OK to not have the () in the new test
instance assignment as long as my constructor takes no parameters.  I added
the (), and didn't get different results.

I'm not sure what you mean when you say Don't you want to  perform your
error-testing functions on the instances?  I'm trying to initialize some
class-arrays in the constructor.  I put some echo statements in there to
try and figure out what was going on.  The example is just an example -- the
smallest piece of code I could make (presumably) that would exhibit the
symptoms.  The purpose for the real class I'm working with is to
encapsulate several arrays (one of which is an array of arrays) along with a
few other variables into a data class that can be put/retrieved as a single
object into the session.  The reason is so that all the code that builds
these arrays only happens when the user hits the first page.  Then the
arrays are available from the session on subsequent hits.

My problem is that when I assign an empty array to a variable, I get
different results depending on whether that variable is in an object or not.
In the case of assigning $a=array(); outside of an object, each element of
the array is empty (as I expected).  But when assigning
$this-$words=array(); results in $this-$words[Amy] (or any other element
for that matter) already being assigned an array.  And if I look at the
elements of those arrays, they are also initialized to arrays.  I don't know
how deep it goes.

The way I noticed this at first was like this:  In the constructor, I'm
reading phrases from a file and putting each word into the $words array, and
assigning $words[$word]=array($phrasenum);  (so each word in the $words
array gives back an array containing the id's of the phrases it appears in)
When I come across a word that I've already added for phrase 1 in phrase 2,
I expect it to already exist in the array, because I put it there the first
time I came across the word in phrase 1.  Instead, Every element is
pre-assigned to another array, and so the code gets confused, thinking that
it has entries for any word I care to index the array with.

Sorry for the long drawn-out message.  Maybe it will help?

Thanks

Brian

Erik Price [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 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



 Erik Price
 Web Developer Temp
 Media Lab, H.H. Brown
 [EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within classes

2002-04-05 Thread Brian McLaughlin

Thank you!!

Now I need to figure out how to put all this hair back in my head.

Brian


Rick Emery [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 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





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php