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.

3
  • what do you mean by "twice" ?
    – galymzhan
    Jan 19, 2011 at 14:50
  • I presume your first programming language is PHP?
    – BoltClock
    Jan 19, 2011 at 14:52
  • His first OOP exposure at least.
    – webbiedave
    Jan 19, 2011 at 14:54

4 Answers 4

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.

3
  • +1, good response. Also want to add another SO question for reference purposes for the OP. Jan 19, 2011 at 14:53
  • So using var is an older practice that isn't necessary anymore?
    – tmartin314
    Jan 19, 2011 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".) Jan 19, 2011 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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.