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

[eluser]Bleeding Edge Productions[/eluser]
Hi guys,

Not that it makes the slightest bit of difference to the execution of the script, but is it better practice to declare members (particularly variables) at the top of a class, or just before they are used?

Either:
Code:
<?php

class Controllername extends Controller {
    
    /**#@+
    * @access private
    * @var string
    **/
    protected $var1;
    protected $var2;
    protected $var3;
    protected $var4;
    /**#@-*/

    function someFunction12()
    {
    $this->var1 = $var1;
    $this->var2 = $var2;
    
    ...more code here...
    }

    function someFunction34()
    {
    $this->var1 = $var3;
    $this->var2 = $var4;
    
    ...more code here...
    }

?>

or:

Code:
<?php

class Controllername extends Controller {
    
    /**#@+
    * @access private
    * @var string
    **/
    protected $var1;
    protected $var2;
    /**#@-*/

    function someFunction12()
    {
    $this->var1 = $var1;
    $this->var2 = $var2;
    
    ...more code here...
    }

    /**#@+
    * @access private
    * @var string
    **/
    protected $var3;
    protected $var4;
    /**#@-*/

    function someFunction34()
    {
    $this->var1 = $var3;
    $this->var2 = $var4;
    
    ...more code here...
    }

?>

(obviously this is a minimal example!)

Cheers,

Martin
#2

[eluser]techgnome[/eluser]
It's probably a matter of preference... but when it comes to PHP, I tend to declare everything at the top. Especially if others are going to use it. Seems to me, that's typically where most people will look first.

-tg
#3

[eluser]Bleeding Edge Productions[/eluser]
Thanks for the reply. That seems to be a pretty common way of doing it.

Do you comment the declarations (as to where they are used in the class / method)?
#4

[eluser]n0xie[/eluser]
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.
#5

[eluser]techgnome[/eluser]
BEP - yes and no... depends on the situation... some of the more complex ones I do... like the definitions of my db record arrays... mostly about what fields I've got loaded at that point, prevents me from trying to access a field I didn't load. I do development professionally (just not PHP) so my philo is to try to name things what they are so that code becomes almost self documenting as much as it can. I don't use junk variables like $i unless I need a quick one for a loop where it doesn't mean much anyways (altho, I'll usually default to $counter).

n0xie - blah! That's cheating! Notepad... that's where the power is at! Wink - disclaimer: I don't use notepad anymore... I've evolved. I'm using Aptana Studio, which is pretty good. It even found some mal-formed HTML I had tucked into some of my PHP code, which I've now gotten straightened out.

-tg
#6

[eluser]OliverHR[/eluser]
Bonus!!????, matter of preference????,

This is all about SCOPE! and memory usage.

http://en.wikipedia.org/wiki/Scope_(programming)
#7

[eluser]techgnome[/eluser]
Given the example where the second set of variables were declared between two functions, scope is irrelevant. If var 3 & 4 were declare inside the function, then scope is a factor. In this contrived example though, all four vars are at the same level (class) and so have the same scope. Now, what I don't remember off the top of my head is if in the "split" case - example 2 - if $var3 and $var4 can be used in someFunciton1 ... something tells me that they should since they are scopped at the class level... which means that the answer is "probably not"... which would be a reason to declare all your vars at the top.

-tg
#8

[eluser]InsiteFX[/eluser]
techgnome is correct!

Variables declared at the Class level are global to that Class!

Variables declared at the method/function level are only avaiable to that method/function.

Unless you use global variables.

InsiteFX
#9

[eluser]OliverHR[/eluser]
The decision to declare variables should not be taken for an ide "bonus", also if any of these variables will be used only(exclusively) within a method, I'm not see why declare them as class properties because this only increases memory usage.
#10

[eluser]n0xie[/eluser]
[quote author="OliverHR" date="1286663679"]The decision to declare variables should not be taken for an ide "bonus", also if any of these variables will be used only(exclusively) within a method, I'm not see why declare them as class properties because this only increases memory usage.[/quote]
Variables are used inside a method and are locally scoped to that method. Hence they are named differently. One usually doesn't 'declare' variables.

The question specifically targeted class properties. By definition these are used throughout your class since they are a member of your class. The fact that they are globally scoped inside your class is probably what you want. 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, he will know what properties he has access to. Declaring them per method is just confusing.




Theme © iAndrew 2016 - Forum software by © MyBB