Citizen
Citizen

Reputation: 12957

Why does the Zend framework prepend an underscore here?

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

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

Upvotes: 2

Views: 538

Answers (2)

Gordon
Gordon

Reputation: 317119

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).

Upvotes: 11

Jordan Ryan Moore
Jordan Ryan Moore

Reputation: 6887

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.

Upvotes: 12

Related Questions