0

What do these var declarations actually do inside of a PHP class:

class VbForumsPageMain extends BxDolPageView {
    var $_oMain;
    var $_oTemplate;
    var $_oConfig;
    var $_oDb;

    function VbForumsPageMain(&$oMain) {
        $this->_oMain = &$oMain;
        $this->_oTemplate = $oMain->_oTemplate;
        $this->_oConfig = $oMain->_oConfig;
        $this->_oDb = $oMain->_oDb;
        parent::BxDolPageView('vb_forums_main');
    }
}

Are they neccessary and do they add any extra use to the variables? I'm not sure whey they're in the class twice.

tmartin314
  • 4,061
  • 10
  • 39
  • 60

4 Answers4

5

The first use is defining them, the second is initialising them. It's good practice to define them up front and even better practice to set the appropriate visibility.

Have a read of the PHP documentation for more information - what you learn now will put you in good stead for the future.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • +1, good response. Also want to add [another SO question](http://stackoverflow.com/questions/1086494/when-should-i-declare-variables-in-a-php-class) for reference purposes for the OP. – Brad Christie Jan 19 '11 at 14:53
  • So using var is an older practice that isn't necessary anymore? – tmartin314 Jan 19 '11 at 14:57
  • @whatshakin Effectively. In modern code, you'd simply use the appropriate public/protected/private visibility. (That said, if you require PHP 4 compatibility, stick with plain old "var".) – John Parker Jan 19 '11 at 14:59
0

This has been answered here, hope it helps - What does PHP keyword 'var' do?.

It basically declares public properties or the class, but you should use private, public and protected instead.

Community
  • 1
  • 1
Matt Lowden
  • 2,586
  • 17
  • 19
0

Yes, they are needed as they define the extension of the class. The VbForumsPageMain class has the same properties (variables) as the BxDolPageView extended by those vars. The function below sets the values and the structure of the class.

crowicked
  • 579
  • 4
  • 6
0

Just to add to other answers: These definitions at the beginning of the class are very important to auto-competition in IDEs. If you don't define the properties at the beginning, the IDE might give you "undefined property" errors. Furthermore it gives you a quick overview of which properties are available in a class.

NikiC
  • 100,734
  • 37
  • 191
  • 225