[UPHPU] Fluent interfaces
David Boucha
boucha at gmail.com
Tue May 8 18:21:29 MDT 2007
On 5/8/07, Richard K Miller <richardkmiller at gmail.com> wrote:
> Has everyone heard of a "fluent interface"? It's a term coined by
> Martin Fowler that I just learned today. (Wondering if I've been in
> the dark about this.)
>
> Instead of returning null from your object setter methods, you return
> a pointer to the object itself. This allows you to chain together
> setter calls. For example, I saw this style when reading the
> documentation for a new version of PEAR Services_Yahoo, which returns
> search results from the Yahoo search engine:
>
> $client = Services_Yahoo_Search::factory("web");
> $results = $client->searchFor("Steve Fossett");
>
> If you want more than 10 search results:
> $results = $client->withResults(20)->searchFor("Steve Fossett");
>
> If you want to start at the 90th result:
> $results = $client->startingAt(90)->withResults(20)->searchFor("Steve
> Fossett");
>
> If you want the results returned in XML:
> $results = $client->withType('XML')->startingAt(90)->withResults(20)-
> >searchFor("Steve Fossett");
>
> Chaining together these options seems to make it pretty readable. Is
> anybody using this style?
>
> Richard
>
Richard,
You can optionally use something like this in the Database class in
the CodeIgniter framework. I know several people on this list don't
like the idea much, but I've found it very useful. I don't use it for
more complicated queries, but for most queries I find it easier to
read and maintain.
Dave
More information about the UPHPU
mailing list