Variable declarations
Moderator: General Moderators
Variable declarations
In PHP it seems most people declare their variables at the top of the script, working with javascript and it's object ( not class based ) inheritance, I've been exposed to another idiom of sorts, building up the properties and methods as go, instead of all the properties and methods separated... is there any pros or cons to doing this in PHP? I guess if you are not using an IDE it would be a pain if the variables weren't in one spot? Then again if the variables are near the method's that use them it would be easier to delete them should the method change, then if they had been at the top? I've never seen anyone not declare their variables at the top in PHP and was wondering if this is the reason?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Variable declarations
Most scripts that i see that are not a class, that have variables at the top is because the variables are meant to be changed aka a public script or its just one file. Now on big projects i have a config file(s)(sometimes folder) that I use.
Re: Variable declarations
I mean in a class
I'm contrasting this style:
with
I'm contrasting this style:
Code: Select all
class Foo
{
protected $a;
protected $b;
public function setA() {}
public function getA() {}
public function setB() {}
public function getB() {}
}
Code: Select all
class Foo
{
protected $a;
public function setA() {}
public function getA() {}
protected $b;
public function setB() {}
public function getB() {}
}
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Variable declarations
I think properties at the top is a good convention because technically properties have a scope of the whole class. Any limits should either be documented or imposed. I would prefer to see either clearly documented:
Or imposed by scope controls:
Code: Select all
class Foo
{
// only for setB() and getB()
protected $a;
// only for setB() and getB()
protected $b;
public function setA() {}
public function getA() {}
public function setB() {}
public function getB() {}
}
Code: Select all
class FooB
{
private $b;
public function setB() {}
public function getB() {}
}
class Foo extends FooB
{
protected $a;
public function setA() {}
public function getA() {}
}(#10850)
Re: Variable declarations
The only case I may separate the variables declarations, is if I have public variables, and the language allows separating method declaration from the implementation.
But I always prefer to have the variables in the beginning, and the function after that. And usually I do not use public variables.
And since in PHP the implementation of the function must follow the declaration, I prefer keep variables in the beginning.
Code: Select all
class A {
protected $a;
protected $b;
protected $c;
protected doSomething();
protected doAnother();
public $d;
public aPublicMethod();
public anotherOne();
}And since in PHP the implementation of the function must follow the declaration, I prefer keep variables in the beginning.
Last edited by Darhazer on Fri Jun 19, 2009 3:09 pm, edited 2 times in total.
Re: Variable declarations
it seems that most fans of the imperative style like to enforce declaration at the top. and i'd say it's good for classes where you're talking about properties/attributes which, as arborint said, have scope for the whole class and thus all methods, you need *somewhere* to declare things that are valid for all methods.
but outside of OO and typed languages i have a different view, and i'd say that perhaps coz JS is outside of those too, you're seeing alternatives.
but outside of OO and typed languages i have a different view, and i'd say that perhaps coz JS is outside of those too, you're seeing alternatives.
Re: Variable declarations
It's probably a hangup from languages like VB where you *must* declare the variable at the top of the current scope.
Re: Variable declarations
PHP will give you an E_NOTICE if you try to use a variable before it's been declared.
Re: Variable declarations
Yeah but the interpreter declares the class variable before any code can even execute, so that wouldnt matter. Just like you can do:
Code: Select all
doStuff(); // I'm using it "Before" its declared, which is actually after chronologically, its only "physically" before
function doStuff() {}
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Variable declarations
I actually like the idea of declaring variables before functions so that function specific variables are declared just before the actual function. This has two benefits. First is that if you decide to remove some parts of the function code (or alter), the declared variables may become obsolete and therefore it is easier to notice the variables getting out-dated. The second thing is that it's faster to declare variables as you code, because you don't have to scroll to the top or collapse loads of methods within a class just to declare an additional variable (or release your hand from the keyboard to use the mouse). Variables that are used in several methods could belong to the top, though.
Btw,
Works just the same way.
Btw,
Code: Select all
class C extends B implements A {}
abstract class B {}
interface A {}Re: Variable declarations
Personally, I put class member declarations at the END of the class. This is simply because I have coded a lot in C++ and it's just familiar to me. Also, I usually skip over these when reading over classes because I care what the class DOES not it's underlying components. Well... at least at first glance! lol
However, I actually prefer when I see a coder who knows how to use interfaces for their objects because you (generally) don't have time to look through the implementation details of everything. You pick up a piece of code and want to know how to use it. After a few iterations, you might want to tinker around under the hood (provided the given software liscence allows you to do such things) to optimize for your specific scenario.
However, I actually prefer when I see a coder who knows how to use interfaces for their objects because you (generally) don't have time to look through the implementation details of everything. You pick up a piece of code and want to know how to use it. After a few iterations, you might want to tinker around under the hood (provided the given software liscence allows you to do such things) to optimize for your specific scenario.
Re: Variable declarations
Well, it's easier to declare as you code, but it's better to declare before you start, because this mean you know what you are going to write. And, class variables are not necessary if they will be used in just one function, they are class variables because they are used by the class, not by single methodkaisellgren wrote:I actually like the idea of declaring variables before functions so that function specific variables are declared just before the actual function. This has two benefits. First is that if you decide to remove some parts of the function code (or alter), the declared variables may become obsolete and therefore it is easier to notice the variables getting out-dated. The second thing is that it's faster to declare variables as you code, because you don't have to scroll to the top or collapse loads of methods within a class just to declare an additional variable (or release your hand from the keyboard to use the mouse). Variables that are used in several methods could belong to the top, though.
Btw,
Works just the same way.Code: Select all
class C extends B implements A {} abstract class B {} interface A {}
And finally, even in one function, I prefer the variables that are used in the function to be in the top... expect for local scope variables (such as counters for loops). In this way you avoid using uninitialized variables. IMHO.
Re: Variable declarations
I get that.. but in some languages, you must declare member variables at the top of your class (i.e. at the top of the current scope.. the scope of the class.)onion2k wrote:PHP will give you an E_NOTICE if you try to use a variable before it's been declared.
In php this isn't an issue.
Code: Select all
class Foo {
public function __construct() {
$this->foo = 'foo';
}
public $foo;
}