Welcome Guest, Not a member yet? Register   Sign In
Declare Members at top of Class or Just Before Use?
#14

[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


Messages In This Thread
Declare Members at top of Class or Just Before Use? - by El Forum - 10-07-2010, 07:06 PM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-07-2010, 07:26 PM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-07-2010, 07:51 PM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-08-2010, 03:25 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-08-2010, 08:36 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-08-2010, 09:41 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-08-2010, 09:55 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-09-2010, 12:29 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-09-2010, 11:34 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-09-2010, 08:45 PM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-11-2010, 09:14 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-11-2010, 10:16 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-11-2010, 11:18 AM
Declare Members at top of Class or Just Before Use? - by El Forum - 10-11-2010, 11:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB