Why does the Zend framework prepend an underscore

2019-07-11 19:10发布

What's the point of having $_comment instead of $comment?

class Default_Model_Guestbook
{
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;
    protected $_mapper;

2条回答
Root(大扎)
2楼-- · 2019-07-11 19:50

Prefixing protected and private class variables is a common convention in PHP. It makes it easier to distinguish between those that are public and those that are protected or private.

查看更多
Explosion°爆炸
3楼-- · 2019-07-11 20:01

Quoted from the Zend Framework Coding Conventions

For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore.

Following a Code Convention means all developers working on a project share the same coding style. This is supposed to make the code more readable and thus easier to work with and on. When working with ZF, you should stick to these conventions as close as possible.

When working with non-ZF PHP code, you are encouraged to use the PEAR PHP Coding Standard instead. Makes life easier for everyone who has to maintain your code (even yourself).

查看更多
登录 后发表回答