简体   繁体   English

声明类变量问题

[英]Declaring class variables question

What do these var declarations actually do inside of a PHP class: 这些var声明实际上在PHP类内部做了什么:

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. 阅读PHP文档以获取更多信息-您现在所学的内容将使您对未来充满信心。

This has been answered here, hope it helps - What does PHP keyword 'var' do? 这已在此处得到解答,希望对您有所帮助-PHP关键字“ var”有什么作用? .

It basically declares public properties or the class, but you should use private , public and protected instead. 它基本上声明了公共属性或类,但是您应该改用privatepublicprotected

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 . VbForumsPageMain类具有与这些var扩展的BxDolPageView相同的属性(变量)。 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. 只是要添加其他答案:在课程开始时,这些定义对于IDE中的自动竞争非常重要。 If you don't define the properties at the beginning, the IDE might give you "undefined property" errors. 如果一开始没有定义属性,则IDE可能会给您“未定义的属性”错误。 Furthermore it gives you a quick overview of which properties are available in a class. 此外,它还为您提供了有关类中可用属性的快速概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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