[eluser]techgnome[/eluser]
I think there's some confusion here...
I don't think any one said that you should declare class level variable just for use inside of a function... Even still ... if you are to use a varaible inside a function, do you declare it at the top of the function, or on the line just before you use it?
So, let's consider a different code example that is less trivial
a User class... with three public properties:FirstName, LastName and EMail.
Let's also say that there are two methods, one for getting the name ,one for the email.
Which of the following would you do it:
Code:
// Option 1
Class User
{
var $FirstName;
var $LastName;
function GetName() {
// assume some call to the database
$this->FirstName = $result['FName'];
$this->LastName = $result['LName'];
}
var $EMail;
Function GetEmail() {
// assume some call to the database
$this->EMail = $result['EMail'];
}
}
- OR -
Code:
// Option
Class User
{
var $FirstName;
var $LastName;
var $EMail;
function GetName() {
// assume some call to the database
$this->FirstName = $result['FName'];
$this->LastName = $result['LName'];
}
Function GetEmail() {
// assume some call to the database
$this->EMail = $result['EMail'];
}
}
The FirstName, LastName and EMail varaibles are accessable outside the class... but where does the declaration of the $EMail belong... between the functions (ie, before it is used) or at the top?
My preference is to keep all my vars at the top of what ever block/scope in which I plan to use it.
-tg