Re: declaring variables in class definitions

From: Date: Wed, 10 Jan 2001 16:44:44 +0000
Subject: Re: declaring variables in class definitions
Groups: php.general 
Request: Send a blank email to php-general+get-33697@lists.php.net to get a copy of this message
Hi Max! --- you wrote: > Hello sam1600, > > Wednesday, January 10, 2001, 6:42:58 AM, you wrote: > > sic> What in the world is reason for declaring the following in the class?: > sic> var $somevar; > sic> I see no reason, and no differences if I don't declare: var $somevar; > > Classes are intended to be structured data storage. You should define > a structure of the class before using the class. > You can set the value of the $somevar anytime later, when using an > object of class a. > $d->somevar = true; > and the next call to $d->b() will print nothing. > But you won't be able to assign a value to $somevar, if it is not > declared in the class. This does not appear to be true. With error reporting set to max, and the variables not defined in the class ( $somevar,$somevar2 and $somevar3 ), the following will echo out: Not set empty value of somevar2 value of somevar3 class a { function b(){ if (!isset($this->somevar)) echo "Not set<br>"; if (empty($this->somevar)) echo "empty<br>"; } } $d = new a; $d->b(); $d->somevar2 = "value of somevar2<br>"; $d->somevar3 = "value of somevar3<br>"; echo $d->somevar2; echo $d->somevar3; > > sic> Maybe a better question is what will break if I do not declare > sic> my vars in my class definition? > > the method b() of the class a will break if you don't declare $somevar > in the class definition. In the string "if (!isset($this->somevar))" > it would throw an error saying that you have no variable named 'somevar' in > you class. This does not appear to be true either, see above. Thanks, Sam I'm using php4.0 ---------------------------------------------------------------- Get your free email from AltaVista at http://altavista.iname.com

« previous php.general (#33697) next »