classes and var declarations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus

    classes and var declarations

    Hello,

    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.
  • Marcus

    #2
    Re: classes and var declarations

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

    Comment

    • Richard Levasseur

      #3
      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
      lifetime.

      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
      them.

      Comment

      • Peter Fox

        #4
        Re: classes and var declarations

        Following on from Marcus's message. . .[color=blue]
        >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.[/color]

        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
        plonker.

        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
        peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
        2 Tees Close, Witham, Essex.
        Gravity beer in Essex <http://www.eminent.dem on.co.uk>

        Comment

        • Andy Jeffries

          #5
          Re: classes and var declarations

          On Sun, 19 Feb 2006 08:48:49 +0000, Peter Fox wrote:[color=blue]
          > Drawback 2 : Lack of access modifiers (you'll get these in PHP 5)
          > private var $bar = 0; // Internal counter[/color]

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

          private $bar = 0; //Internal counter
          [color=blue]
          > Drawback 3 : Lack of type checking (or something similar in ver 5)[/color]

          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.

          Cheers,


          Andy


          --
          Andy Jeffries | gPHPEdit Lead Developer
          http://www.gphpedit.org | PHP editor for Gnome 2
          http://www.andyjeffries.co.uk | Personal site and photos

          Comment

          Working...