views:

154

answers:

2

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

class Default_Model_Guestbook
{
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;
    protected $_mapper;
+10  A: 

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.

Jordan Ryan Moore
Notable is that PEAR2 has dropped the convention. And ZF2 is conducting a community poll to determine whether or not they, too, will drop it.
David Weinraub
+10  A: 

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

Gordon
Beat me by a second. That's a good reference for understanding 'why' some coding style is used in the Zend Framework.
Tim Lytle
Well, it doesnt explain so much why it is prefixed. It just tells us it should be :) But you learn to appreciate it, once you've seen legacy code from developers following their own chaos-driven code conventions.
Gordon
The other question answered what I was looking for, but I upvoted you as well :)
Citizen