Re: Re[2]: [PHP] declaring variables in class definitions

From: Date: Wed, 10 Jan 2001 18:55:34 +0000
Subject: Re: Re[2]: [PHP] declaring variables in class definitions
Groups: php.general 
Request: Send a blank email to php-general+get-33739@lists.php.net to get a copy of this message
Max, Thanks for your response. Assigning something to $d->somevar when it is not declared in the definition works fine. As far as I can tell, and from what we have found here, there is absolutly no reason whatsoever to define variables in class definitions. If someone can show me me a reason , other than a pure aesthetics, why this is nessesary I would sure like to know. If anyone could show me at least one example where not defining variables in class definitions would somehow break the class, I would like to see it. Thanks, Sam >> $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. sic> This does not appear to be true. With error reporting set to max, and the sic> variables not defined in the class ( $somevar,$somevar2 and $somevar3 ), sic> the following will echo out: sic> Not set sic> empty sic> value of somevar2 sic> value of somevar3 sic> class a { sic> function b(){ sic> if (!isset($this->somevar)) sic> echo "Not set<br>"; sic> if (empty($this->somevar)) sic> echo "empty<br>"; sic> } sic> } sic> $d = new a; $d->b(); $d->somevar2 = "value of somevar2<br>"; $d->somevar3 = "value of somevar3<br>"; echo $d->somevar2; echo $d->somevar3; try to assign something to $d->somevar if it is not declared in the 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. sic> This does not appear to be true either, see above. bad practice anyway ---------------------------------------------------------------- Get your free email from AltaVista at http://altavista.iname.com

« previous php.general (#33739) next »