I’ve seen many times Zend Framework using return $this; pattern style
– and from my point of view:
-
Pro: seems its quite not bad
patternstyle for chaining many actions on the same object and making your code shorter. -
Con: code looks a bit weird when you see that object returns itself in the method, which does something else (e.g. setter for some property)
Is it really good pattern practice or maybe an anti-patternpractice?
EDIT: well it was a little too much from my side to call it “pattern”, thanks everyone for pointing me to right direction!
Returning
thisallows you to chain calls and set values. It is very useful for configuring some object (see Fluent interface). You can express very easily what you want (and you can use different return types to achieve what you want).