@chazzyFeb 25.2008 — #Do you mean something like this?
[code=php] class MyClass { var $var1; var $var2; var $var3;
function myFunction1() { $anotherVar = 0; } } [/code]
The difference is that $var1/2/3 represent "members" of the class - they exist as long as an instance of the class is created. you'll hear them called instance variables as well.
$anotherVar is different, it only exists inside the method.
When to use one or the other is dependent on the scope you want to use.
[code=php] class MyClass { var $var1; var $var2; var $var3;
function myFunction1() { $anotherVar = 0; } } [/code]
The difference is that $var1/2/3 represent "members" of the class - they exist as long as an instance of the class is created. you'll hear them called instance variables as well.
$anotherVar is different, it only exists inside the method.
When to use one or the other is dependent on the scope you want to use.[/QUOTE]
@ZnupiFeb 25.2008 — #I only declare variables at the top of the class when I need them to have a default value, like this: [code=php] class Something { var $myVar = "default value"; } [/code] Otherwise, it's perfectly safe to do: [code=php] class Something { function myFunc() { $this->myVar = "some value"; } } [/code] ?
@TJ111Feb 25.2008 — #I declare all of the ones that are predefined at the top, or if I need to set the scope (or both). Keep in mind if you are using PHP5 you should use 'public/private/protected' instead, as 'var' is PHP4 syntax and might not be supported in PHP versions. Also I list out any variables that are created in the __construct function. Its good practice to be write out all the variables you use at the top, its like a quick cheat sheet for the class. [code=php] Class MyClass { public $var1 = 18; protected $var2 = "19"; private $var3 = "private";