classes and var declarations

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I've read many places on the net that when using classes in PHP, all  
member variables must be declared via the 'var' keyword.

I know that doing this creates all these variables when the object is  
first created.

My question is what is the point of this and is it bad if I don't  
declare all my variables at the beginning?

Right now when I have a variable I want to save in the object globally I  
just assign $this->variable whenever I need it.  I am  
serializing/unserializing my object in $_SESSION between page calls.  
Everything is working fine, and this is actually better for me, because  
I do not want all my variables to be created at the start, but instead  
as I go based on different function results (I do not necessarily want  
every possible variable in my class to be set on each run).

So to summarize, are there any problems with simply assigning  
$this->variable whenever I need, instead of explicitly declaring all  
variables with 'var' at the beginning of my class?  I have found many  
sites telling me I have to declare them, but most don't explain why, and  
I have not had any luck finding any documentation on potential drawbacks  
  with what I am doing.

Thank you all in advance.

Re: classes and var declarations

I forgot to mention in my previous post that I am using PHP 4.3, thanks!

Re: classes and var declarations

It is good programming practice to declare all the class and object
members at the start.  That way, when you or someone else goes back to
modify it 6 months later you have a better idea of what is going on.

Secondly, if you were to receive an arbitrary object of that type, you
know what is available to use without having to rely on some technique
to tell you what was set/not-set during the course of the objects

If you have a lot of variables in a class, but are only using a few at
a time, then you may want to reconsider the object and the breakdown of

Re: classes and var declarations

Following on from Marcus's message. . .
Quoted text here. Click to load it

There's no gain so avoid the drawbacks.

Drawback 1 : Lack of clarity
   var $fooString = '';  // Used to hold results of Foo()
Not only does this tell you in the code what's what but also a  
documenter can pick it up.

Drawback 2 : Lack of access modifiers (you'll get these in PHP 5)
   private var  $bar  = 0;  // Internal counter

Drawback 3 : Lack of type checking (or something similar in ver 5)

Drawback 4 : Anyone looking at your code will think you're a right  

Initialising doesn't cost much when compared to the time spent debugging  
code.  You can start with a null value.

If your class is accumulating loads of unused variables then you  
probably want to look again at your class structure or data model.

PETER FOX Not the same since the deckchair business folded
2 Tees Close, Witham, Essex.
Gravity beer in Essex  <

Re: classes and var declarations

On Sun, 19 Feb 2006 08:48:49 +0000, Peter Fox wrote:
Quoted text here. Click to load it

Odd, I've never seen it written for PHP5 like that, it's normally:

private $bar = 0; //Internal counter

Quoted text here. Click to load it

How do you do that?  I know you can use Class Hints in function calls
(e.g. function foo(MyClass $bar)) but not how you can type check a loosely
typed language.

That said, I agree with your overall comment, it does make more sense to
include them at the top of the class definition to make it more obvious
which variables are definitely being used and which ones are accidental.



Andy Jeffries                 | gPHPEdit Lead Developer | PHP editor for Gnome 2 | Personal site and photos

Site Timeline