Just wondering: In yii, some of the class fields are named as

public $_files; public $_name; 

Others without underlining

 public $db; 

What is the difference?
Fields marked with underscore are metadata?

  • and from where these fields are torn out? - IVsevolod
  • @IVsevolod from context :) - zenith

1 answer 1

Usually, underlined variables start protected variables. It is customary to write a setter and a getter to these variables, since out of no access to them.

Example:

 protected $_name; public function getName() { return $this->_name; } public function setName($name) { $this->_name = $name; return $this; } 
  • Just do not return #this; , and return $this; :-) - Johny
  • 2
    not only protected, but private =) - mountpoint
  • 2
    Just add. It went with PHP 4 when there were no access modifiers, and thus they were determined to visually see what it belongs to. Well, now it's just someone out of habit is using. - aldem67
  • 2
    And I will add a little bit too: this is not only in PCPs, for example, in python it is common practice for protected variables (avoids name mangling), in sisharp it is often what property holders are called (property in sisharp is a getter and setter , very similar to the above situation, but there everything is smarter). In general, the deeper I dig Yii and C #, the more obvious for me is the fact that Yii is not actually written by PHP. - etki
  • one
    @ aldem67 I’ll add a bit more :) for some frameworks (eg Zend Framework 1) it’s also an accepted coding standard - zippp