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

From: Date: Wed, 10 Jan 2001 20:31:17 +0000
Subject: Re: Re[2]: [PHP] declaring variables in class definitions
Groups: php.general 
Request: Send a blank email to php-general+get-33771@lists.php.net to get a copy of this message
Hi Toby, >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. Defining variables with (var $myVar;) in a class does not initialize that variable in any way. It still does not exist in any way whatsoever, period. I repeat, var $myVar; does NOT set the varable If the variable was set I could see a reason for the defining, but because the variable is not set and does not exist, after (var $myVar;), there is no reaon for this other that commenting your code as you said. But on the otherhand if php is just showing us a way to define default values for variables outside of class methods like (var $myVar = "somevalue";) which ofcource works , thats just fine, but just sticking (var $myVar;) in there is just useless. >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. In each method of the class you still have to reference the object variables with a $this->myVar, whether the $myVar is deifined in the class with (var $myVar;) or not ( unless you are wanting to use a variable local to the method ofcource which is another matter ). So declaring (var $myVar;) in the class will not stop you from missnameing variables and also defining (var $myVar;) does not set the variable, in fact the variable still does not exist in anyway whatsoever. I hope you understand what I'm saying. Sure, defining variables in the class is a nice way to organize your code. But if (var $myVar;) does absolutly nothing whats the point. If I want to comment my code I can do: // note to Sam: don't misspell $myvar ;-) Thanks, Sam ---------------------------------------------------------------- Get your free email from AltaVista at http://altavista.iname.com

« previous php.general (#33771) next »