Method chaining, aka. fluent interface, can be found in some places in Yii, such as the new query builder, where one can chain several method calls together, like the following:
I agree where the return of the object (aka chaining) makes sense I will let it limited. The rest I am not very sure and I agree with most of your concerns.
Noone forces to use method chaining and everything stays backwards compatible as it was before. If you see that method chaining makes sense you’ll use it if not, just don’t use it.
From my point of view considering to return $this or not is relatively simple. We should return $this if:
— Method doesn’t return anything else.
— Method is not the final step in object’s lifecycle. Such method is, for example, CApplication::end().
Cons:
It does not feel absolutely right but I can’t find any.
Antonio Ramirez
Any specific classes where you are missing chaining?
I wonder what will happen with method chaining, if there’s a setter e.g. setSomething($val) which returns $this:
$x = $component->something = 'bla';
What will be in $x, if the setter returns $component itself?
Besides unclear cases like this, I think i agree with Qiangs concerns:
It would look like Yii encourages the use of method chaining. It might have no impact at all, but things often develop some self-perpetuating dynamics and suddenly everyone is chaining like crazy. Which then again could have side effects like hindering the implementation of some specific new features, that would break chaining (and thus break BC).
All has been written already… I too think that chaining should be limited…
if we just add it everywhere… it can in the end bring to inconsistency as some methods return a "custom" value and some would return $this… this way users/programmers would need to check the documentation even more than now (to see what is the method returning)
I’m not objectionable to method chaining but do agree it needs to be used where it makes calls to that code easier to read. Like with the new command builder because all the calls are related to one another. Where two consecutive calls are made but are unrelated then method chaining doesn’t make sense.
I’ve always wondered however what the result of excessive method chaining does to the stack?
The idea that there is more than one way to ‘do it right’ arouses my attention, as — just like practice shows — it makes simple things complicated. Because, as you add such a feature in each and every “void” & “not-final” method, immediately someone would try to benefit from it, and, still there will be those who won’t bother. Such diversity frightens already. More parties — more tension.
Not to say that, there is a hypothetical chance for those, who’ll follow the chaining way, to break their legs trying to make a chain, paying each and every attention to consider whether this or that method is chainable.
So my phobic vote is ‘No’, for chaining approach should be introduced wisely, in a limited fashion %)
I’d say that adding method chaining to every component is too much. Not only it will put more strain on developers to code new components in such way, but all extensions should then follow that method to - and that obviously will not be done in most cases, so we will have some nightmare code like
there may be potential unforeseen backward compatibility problems as mentioned above
debugging is somewhat harder now, for example, if a problem is somewhere in the middle of the chain, you will most likely need to tear apart the chain (i.e. write them out line by line or split into non-chained calls) to find the problem.
I think if chaining is desired, it needs to be planned and designed from the beginning
I totally agree with that, such things, even though they are useful in certain scenarios, should be designed from the beginning and not added late, when there is risk of backward compatibility.
But since Yii 2.0 will be rewritten entirely maybe chaining would be a nice feature.