Page 1 of 2

Chaining Methods

Posted: Tue Oct 14, 2008 2:17 am
by VirtuosiMedia
Does anyone chain their methods? Could you explain why you chose or chose not to use chaining? I use chaining in JavaScript all the time, but I haven't really used it very much in PHP, so I'm wondering at people's experiences. Has it improved your code's readability? What are the drawbacks, advantages, and things to watch out for? What are the best use cases and best practices? I'm using this write-up as a launching point, but if you find it inaccurate in any way, please point it out and why.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 4:49 am
by Christopher
I think chaining is horrible. You end up with order dependent ... essentially procedural code. I did it in one code base and it has been a pain to refactor out. Doing a forward on some condition is one thing, but chaining to build program flow is a mess in my opinion.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 5:19 am
by Eran
I might be wrong, but I think VirtousiMedia was refering to chaining as in fluent interface, where methods return the invoking objects if there is no specific return value to return.
I use that constantly for invoking several methods (regardless of order) without writing the object name each time. I find it somewhat speeds up the actual coding and it leaves better looking in my opinion. It's really a matter of preference, and I'm used to it from jQuery as well (which has a brilliant interface).

Re: Chaining Methods

Posted: Tue Oct 14, 2008 6:17 am
by onion2k
Chaining is nice... if the language lets you do it in a nice way. In PHP it ends up being a horrific hack. I'd rather settle for a 'normal' set of individual calls than bodge the code to force an interface just because you can.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 6:40 am
by Eran
What do you mean by bodge the code? what's wrong with returning the calling object and how is it a hack exactly?

Re: Chaining Methods

Posted: Tue Oct 14, 2008 6:46 am
by alex.barylski
I believe (in most cases) many developers consider chaining the same things as fluent interfaces.

Under certain circumstances I find it improves the readability of your code. It also comes in handy when attempting to emulate a DSL using native PHP.

SQL or BDD could use fluent interfaces to essentially express a declarative statement using PHP so you avoid introducing 'yet another compiler compiler' :P

I think for everyday programming...it's probably not the clearest option.

Although come to think about it...you know...if you ever had to enforce the exact execution of methods...chaining would be the way to go wouldn't it. A developer is far less likely to remove a method call from a chain in attempt to optimize or streamline than he/she would if the invocations were on individual statements.

Then again...debugging becomes slightly more difficult as you cannot echo the results of just a single method or a few consecutively. You would have to comment everything after the method in question and close the statement, not difficult but not as easy as using single statements.
In PHP it ends up being a horrific hack. I'd rather settle for a 'normal' set of individual calls than bodge the code to force an interface just because you can. Chaining is nice... if the language lets you do it in a nice way. In PHP it ends up being a horrific hack. I'd rather settle for a 'normal' set of individual calls than bodge the code to force an interface just because you can.
Horrific hack? How so? How would you do it better?

Re: Chaining Methods

Posted: Tue Oct 14, 2008 9:22 am
by onion2k
PCSpectra wrote:Horrific hack? How so? How would you do it better?
In other languages you get define methods that are accessed depending on how the object is used... for example if an object is used in the context of a string then _toString() is called rather than you having to implicitly call that function as part of the chain. That's much neater because you don't have to mess around passing object references around the place. It just works.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 9:42 am
by Eran
I didn't quite get that.. can you give an example? All the languages I'm familiar with do implement fluent interface the same as PHP (by returning the object instance).

Re: Chaining Methods

Posted: Tue Oct 14, 2008 9:46 am
by VladSun
I think, onion2k meant:

Code: Select all

C#
Int32 i = 64;
String s = "This is the string presentation of i" + i;
 
Which is the same as:

Code: Select all

C#
Int32 i = 64;
String s = "This is the string presentation of i" + i.ToString();
 

Re: Chaining Methods

Posted: Tue Oct 14, 2008 10:06 am
by Eran
No, that's operator overloading which is something different.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 10:46 am
by Selkirk
I think one thing that gets overlooked sometimes in designing the fluent interface is actually keeping the order or the calls making sense. The whole chaining statement from the first call to the last call should make sense as one unit of work. This paper explains designing a fluent interface in Java where they used interfaces to maintain context down the call chain. I think the cool thing about that is they get some IDE assistance while you are using the greater fluent interface. I've also done some stuff with a small state machine built into a single class that disallows invalid combinations and forces an order on the statement.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 1:21 pm
by VirtuosiMedia
pytrin wrote:I might be wrong, but I think VirtousiMedia was refering to chaining as in fluent interface, where methods return the invoking objects if there is no specific return value to return.
I use that constantly for invoking several methods (regardless of order) without writing the object name each time. I find it somewhat speeds up the actual coding and it leaves better looking in my opinion. It's really a matter of preference, and I'm used to it from jQuery as well (which has a brilliant interface).
I did mean chaining as in fluent interface, though I'm not quite sure what other meanings it might have. I use it with the MooTools JavaScript library all the time and was just curious about its use in PHP since I saw that you could do something similar.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 1:37 pm
by webaddict
VirtuosiMedia wrote:I did mean chaining as in fluent interface, though I'm not quite sure what other meanings it might have. I use it with the MooTools JavaScript library all the time and was just curious about its use in PHP since I saw that you could do something similar.
I like fluent interfaces, because they indeed can shorten the syntax and therewith make your code clearer. Also, complex calls can be accomplished in a single call. Actually, I'd like PHP to return the newly created object on instantiation, to make objectchaining possible (eg. $object = new MyObject( )->activate( )). A thing that is important to keep in mind though (like arborint already said) is that order shouldn't matter, since that would promote procedural usage of your objects, which obviously is a bad thing. A second thing to keep in mind is the law of demeter.

Re: Chaining Methods

Posted: Tue Oct 14, 2008 4:24 pm
by alex.barylski
In other languages you get define methods that are accessed depending on how the object is used... for example if an object is used in the context of a string then _toString() is called rather than you having to implicitly call that function as part of the chain. That's much neater because you don't have to mess around passing object references around the place. It just works.
I have no idea what your talking about...if you call an object in the context of a string...and it returns a string and not the object...I fail to see how that has anything to do with fluent interfaces. Sounds more like operator overloading -- which is a nice feature but totally different.

I've often thought that with operator overloading and fluent interfaces you could really construct some interesting DSL solutions.

Re: Chaining Methods

Posted: Wed Oct 15, 2008 2:27 am
by webaddict
onion2k wrote:In other languages you get define methods that are accessed depending on how the object is used... for example if an object is used in the context of a string then _toString() is called rather than you having to implicitly call that function as part of the chain. That's much neater because you don't have to mess around passing object references around the place. It just works.
I guess what one calls chaining methods, the other calls operator overloading. The above description sounds like type polymorpism to me, which has nothing to do with my understanding of chaining methods.

Chaining methods is:

Code: Select all

 
<?php
// normal.
$myobject = new MyObject( );
$myobject->doThis( );
$myobject->doThat( );
 
// "chaining"
$myobject = new MyObject( );
$myobject->doThis( )->doThat( );