Chaining Methods

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Chaining Methods

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Chaining Methods

Post 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.
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Chaining Methods

Post 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).
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Chaining Methods

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Chaining Methods

Post 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?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Chaining Methods

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Chaining Methods

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Chaining Methods

Post 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).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Chaining Methods

Post 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();
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Chaining Methods

Post by Eran »

No, that's operator overloading which is something different.
Selkirk
Forum Commoner
Posts: 41
Joined: Sat Aug 23, 2003 10:55 am
Location: Michigan

Re: Chaining Methods

Post 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.
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: Chaining Methods

Post 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.
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Re: Chaining Methods

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Chaining Methods

Post 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.
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Re: Chaining Methods

Post 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( );
 
Post Reply