Drupaler
Drupaler

Reputation: 19

Difference b/w Single and double underscore in PHP Zend Framwork

I am a new be to start work on zend framwork. i got an example quick start from zend site. when i review the code then i found single and double underscore that is specious to me. below is the code...


1.
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;

2. 
   public function setComment($text)
    {
        $this->_comment = (string) $text;
        return $this;
    }
3.
    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');
        }
        $this->$method($value);
    }

Upvotes: 0

Views: 685

Answers (2)

Liyali
Liyali

Reputation: 5693

You should take a look on this:

PHP Magic Methods

Caution
PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

Zend Framework Naming Conventions

For methods on objects that are declared with the "private" or "protected" modifier, the first character of the method name must be an underscore. This is the only acceptable application of an underscore in a method name. Methods declared "public" should never contain an underscore.

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.

Upvotes: 3

Dunhamzzz
Dunhamzzz

Reputation: 14808

A double underscore represents a PHP Magic Method, where as a single underscore (by convention only) denotes a class method or property is private or at least protected.

For example you'd expect

protected function _doThis() {}

But not

public function _doThat() {}

Upvotes: 1

Related Questions