Variable declarations

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Variable declarations

Post by josh »

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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable declarations

Post by Christopher »

Do you mean properties in a class or just any script?
(#10850)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Variable declarations

Post by Zoxive »

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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable declarations

Post by josh »

I mean in a class

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() {}
}
 
with

Code: Select all

 
class Foo
{
 protected $a;
 public function setA() {}
 public function getA() {}
 
 protected $b;
 public function setB() {}
 public function getB() {}
}
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Variable declarations

Post by Christopher »

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:

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() {}
}
 
Or imposed by scope controls:

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)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Variable declarations

Post by Darhazer »

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.

Code: Select all

 
class A {
   protected $a;
   protected $b;
   protected $c;
 
   protected doSomething();
   protected doAnother();
 
   public $d;
   
   public aPublicMethod();
   public anotherOne();
 
}
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.
Last edited by Darhazer on Fri Jun 19, 2009 3:09 pm, edited 2 times in total.
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: Variable declarations

Post by Popcorn »

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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Variable declarations

Post by Jenk »

It's probably a hangup from languages like VB where you *must* declare the variable at the top of the current scope.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Variable declarations

Post by onion2k »

PHP will give you an E_NOTICE if you try to use a variable before it's been declared.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable declarations

Post by josh »

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() {}
 
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Variable declarations

Post by kaisellgren »

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,

Code: Select all

class C extends B implements A {}
abstract class B {}
interface A {}
Works just the same way.
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: Variable declarations

Post by jgadrow »

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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Variable declarations

Post by Darhazer »

kaisellgren 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,

Code: Select all

class C extends B implements A {}
abstract class B {}
interface A {}
Works just the same way.
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 method
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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Variable declarations

Post by Jenk »

onion2k wrote:PHP will give you an E_NOTICE if you try to use a variable before it's been declared.
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.)

In php this isn't an issue.

Code: Select all

class Foo {
  public function __construct() {
    $this->foo = 'foo';
  }
  public $foo;
}
Post Reply