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

From: Date: Wed, 10 Jan 2001 19:40:19 +0000
Subject: Re: Re[2]: [PHP] declaring variables in class definitions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-33748@lists.php.net to get a copy of this message
They don't break the classes per se, but it definately promotes good OOP structure. Keep this in mind for PHP 5 and PHP 6 which might (who knows) bring us more strict OOP rules (ie private and public variables). In which case your classes would have to be entirely rewritten instead of just putting a public: or private: in front of your already defined variables. --Joe On Wed, Jan 10, 2001 at 01:55:34PM -0500, sam1600@iname.com wrote: > > 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net --- Joe Stump PHP Programmer www.Care2.com

« previous php.general (#33748) next »