RE: [PHP] Help with associative array in a PHP class. Not working as I'd expect it to?!

From: Date: Thu, 11 Dec 2003 03:43:35 +0000
Subject: RE: [PHP] Help with associative array in a PHP class. Not working as I'd expect it to?!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-172430@lists.php.net to get a copy of this message
Grrr what a newbie mistake (about the scope I mean)! I guess I assumed that in a class, the $this-> was redundant for a method (it is in J++ which is where I learned my OOP). So all my variables in the main class were 'global' to methods in the class. My second bit of confusion is that I've made classes before where I do this: class myclass { var $five = 5; ... } And it works fine, so you can see how the subtle difference (no var keyword basically) could lend to confusion. I still don't fully agree with the fact that I can't initialize the array key/value like that. It seems to me logical that it should work after I declared the array already. class myclass { var $myPacket = array(); $myPacket["blah"] = "asdfasdfasdf"; ... } Thanks guys! > -----Original Message----- > From: John W. Holmes [mailto:holmes072000@charter.net] > Sent: Wednesday, December 10, 2003 6:54 PM > To: Daevid Vincent > Cc: php-general@lists.php.net > Subject: Re: [PHP] Help with associative array in a PHP > class. Not working as I'd expect it to?! > > Daevid Vincent wrote: > > > myPacket: > > ------ > > > > How come I don't get ANY array under "myPacket:"? > [snip] > > function printMyVars() > > { > > echo "myPacket:\n"; > > print_r($myPacket); > > } > > Two words: Variable Scope. :) > > $myPacket is local to the function and empty. > > -- > ---John Holmes... > > Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ > > php|architect: The Magazine for PHP Professionals - www.phparch.com > > > >

« previous php.general (#172430) next »