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

[eluser]OliverHR[/eluser]
[quote author="n0xie" date="1286696727"]One usually doesn't 'declare' variables.[/quote]
I mean declaring in general programming terms. Declaring Variables

[quote author="n0xie" date="1286696727"]The right way to do it, is to declare them at the top of your class so that whenever another developer looks at your class.[/quote]

I think is better to use comments for this.

Anyway it is my point of view.
#12

[eluser]n0xie[/eluser]
[quote author="OliverHR" date="1286828043"][quote author="n0xie" date="1286696727"]One usually doesn't 'declare' variables.[/quote]
I mean declaring in general programming terms. Declaring Variables
What use would declaring a variable have in a dynamic weak typed language?

[quote author="n0xie" date="1286696727"]The right way to do it, is to declare them at the top of your class so that whenever another developer looks at your class.[/quote]

I think is better to use comments for this.
[/quote]
You use comments to declare class properties?
#13

[eluser]OliverHR[/eluser]
[quote author="n0xie" date="1286831764"]What use would declaring a variable have in a dynamic weak typed language?[/quote] Man I never say that, you Do
[quote author="n0xie" date="1286547958"]If you declare them at the top, you get the added bonus that your IDE will pick them up and will help you when using them.[/quote]

Even I think it is the right way no matter if php, vb6 an so... let to put variables in any place. what I say its if a variable its only used inside a method whay "Declare" it as a class property.

[quote author="n0xie" date="1286831764"]You use comments to declare class properties?[/quote]You are confused.

Anwser: No, I not, comment everything, but beacuse I forget to do.

But in my work I always must comment everything.(If I miss to comment my code, I could get fired)
Code:
...

/**
* example of documenting a variable's type
* @var string
*/
var $variable;
...
/**
* Bla, bla...
* @param  datatype  $paramname  description
* return  ...
*/
function do_some($paramname)
{
// some info that help
$othervar

return ...
}

???

As I say It is my point of view, I am not forcing you to do things like me.
#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




Theme © iAndrew 2016 - Forum software by © MyBB