简体   繁体   中英

Declaring class variables question

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM