Maugrim_The_Reaper wrote:These aren't really related. They were thrown together because it looks neat. Functionally it certainly works, but is it a good idea to attach formatting and pricing together?
Thats basically what I've been asking myself...while I agree they are not related but it looks neat...
It does (when formatted correctly) allow you to group multiple method calls under a single object reference:
Code: Select all
$obj->doSomething()
->doSomethingAgain()
->oneMoreTime();
I see this as a similar technique to VB and it's 'with' statement which saves on typing the $object identifier over and over again. On one hand, although I don't find the code much clearer, it is clearer in the sense there is less to look at. You also avoid possible typo-errors of typing
$ojb and
$obj.
On the flipside, it's now impossible to group related statements inside a conditional, whereas it's a trivial matter if they are their own individual statements...
A simple example using PHPMock might look like:
That is probably the best example of usage. Ok, you have essentially nailed the three uses that I seen for fluent interfaces. I have up til now, used fluent interfaces for accessing parent or composite objects, as opposed to accessing the members directly - I set all member variables to private so once I switched to PHP5 it was a given.
For for grouping the method calls of an object is warranted or not...I think if you look at from the perspective that the methods are not related and should be separated into individual statements, then it makes sense to keep them separate (also like I said, it's impossible to execute those statements conditionally now). However, from a slightly higher perspective 'with proper formatting' I can also see it as being a benefit. I guess the latter is purely personal taste as it's more to do with reducing typing and increasing clarity strictly through formatting.
Fluid interfaces make me smile. I love not having to store every damn thing in variables and pass them around.
That is sort of the direction I'm heading in...
VB6 also has similar, using the with statement:
That is basically what it reminded me off when I looked at it from an usage of something other than just method chaining...and I thought...cool...I remember using that in VB4
