PHP 5 and the var keyword

Object-oriented support in PHP has been greatly improved with release 5. However, for backward compatibility reasons with older versions, some coding practices have been keeped. Among them there's the use of the var keyword in class definitions. Honestly, from an OOP perspective, I don't understand what this keyword is useful for. For example:

class myClass 
{
  var foo;
  var bar;
  var baz;
}

Seeing these declarations gets me confused, because I don't know at first glance what's the visibility of these members. Using a classic OOP, instead, makes life a lot easier and it's really much neater:

class myClass
{
  public $foo;
  protected $_bar;
  private $_baz;
}

So far I've never used var in my classes, and I have to say that my code is clearer and more intuitive. You should take into account the fact of getting rid of this old habit.

Leave a Reply

Note: Only a member of this blog may post a comment.