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

From: Date: Wed, 10 Jan 2001 19:40:51 +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-33749@lists.php.net to get a copy of this message
This isn't really aesthetics as much as it is organization. You already know that you don't have to declare PHP variables... if I had a one-line script that simply said: <?php echo $myVar++ ?> I would end up with 1 as the output. myVar would be created as soon as PHP encountered it (it would be empty, however), and with the ++ the variable would be type-casted to a numeric type (yeilding 0) and 1 would be added. Thus you get 1 as your output. Likewise, if you reference a non-existant variable that is within a class, PHP initializes the variable with an empty value. You're going to end up with a variable, not an error, whether it's explicitly defined in the class or not. However, here's why it's not just aesthetics: If you have a lot of code to go through, organization is important. If you declare a class and only define some functions (without explicitly defining variables with var $myVar, etc.), you could very well end up wondering what variable is what and whether a variable exists, etc. It wouldn't be too hard to stray from the right path and end up with half of your variables misnamed. -Toby ----- Original Message ----- From: <sam1600@iname.com> To: <php-general@lists.php.net> Sent: Wednesday, January 10, 2001 1:55 PM Subject: Re: Re[2]: [PHP] declaring variables in class definitions > > 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 > >

« previous php.general (#33749) next »