What do you think about writing own classes in the manner jQuery / Code Igniter (and probably many more) are written (look here, example in section "Email and Validation chaining")? I mean that each or most methods returns $this (class instance itself), so user can call "nested" calls like that (example from above mentioned page):
This is the way how for example CDbCommand is written in Yii, but if I’m not mistaken, most extensions / user code and many other Yii classes does not introduces this behaviour.
What do you think about this style of coding? Do you know any pros or cons? Is is a best practice? In what situation this approach should be and when shouldn’t be used?
Totally agree with you. The question is how to easily determine, if current method should be written in the way so it could be used in chain-like style or not? :]
Provided CodeIgniter code is a good example. It uses chaining while many other mailing classes from many other frameworks (or own cooked) doesn’t. What are circumstances to determine, which approach is better and when?
When Building some kinda of data with different pieces for example the email one u provided above, building queries… where your methods do not need to return some kind of meaningful value e.g boolean checks (i know you would usually do this with get* methods).
say for example you have a class HtmlPage, and functions like setDoctype, setTitle, addScript, setHeader, setContent, setFooter… You would need to call all of these functions to build an "HtmlPage" and none of these functions needs to return anything special, then it would make sense to chain them.
I don’t suppose you would want to chain get* methods since you would usually expect them to return something eg:: User::getId()…