DanielMorgan

Web Developer

Jul.10.13

A Fluent interface is a way to chain code together to make statements more comprehensive and understandable. Looking at the Wiki page you can tell that there is no examples in PHP. It *IS* possible to create a fluent interface in PHP if you return the object on each function.

It is important to note that for fluent interfaces, you want to assume any action is being taken on the last “subject”. Here is a simple class that demonstrates this principle :

It starts with the static function “chain”. This returns a object that can be chained to other methods in the chain singleton. I choose to push and pop variables as a way to keep focus on the immediately added number. For example :

T::insert(4)->
add(2)->
insert(12)->
insert(2)->
add(1)->
results();

Would produce the results :

0 : 6
1 : 12
2 : 3

To make my life easier, I decided to create a singleton helper class. This would allow you to register, tab through ( via the point() function ), and execute commands chaining from the source.

PHPChain can also be extended to be a generator, or the base of a whole new framework. I like to pair it to request routers such as the AltoRouter. Here is what serving up a webpage would look like in chains with the AltoRouter via anonymous functions :