Why return $this in setter methods?

后端 未结 2 1929
挽巷
挽巷 2021-01-01 16:06

Examining Zend Framework, I found that all setter methods (of those I’ve examined) return the instance of the class it lives in. It doesn\'t only set a value but also return

2条回答
  •  梦毁少年i
    2021-01-01 16:41

    It's so that method calls on an object can be "chained", like this.

    $obj -> setFoo ('foo') -> setBar ('bar') -> setBaz ('baz') -> setFarble ('farble');
    

提交回复
热议问题