All Together, As A Whole

Independent Developer and Web Site Builders Network

What is Fluent Interface?

A fluent interface is normally implemented by using method chaining to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining). Generally, the context is

* defined through the return value of a called method
* self referential, where the new context is equivalent to the last context
* terminated through the return of a void context.

This style is beneficial due to its ability to provide a more fluid feel to the code.

Example:

Today, any developer needs a service to send mail. Do you have a samething implement on many Classes, phpMailer, etc. When you have to know new framework/application/class do you need understand how works, what methods, whenever.. Usign Fluent Interface we can construct a Abstract Layer with have specific terms used in any class with implements email service.

class FluentEmail
{
private $_to;
private $_from;
private $_subject;
private $_body;

public function to($to)
{
$this->_to = $to;

return $this;
}

public function from($from)
{
$this->_from = $from;

return $this;
}

public function subject($subject)
{
$this->_subject = $subject;

return $this;
}

public function body($body)
{
$this->_body = $body;

return $this;
}

public function send()
{
//using php mail
mail($this->_to,$this->_subject,$this->_body);
//or using framework
JUtility::sendMail($this->_from,$this->_from,$this->_to,$this->_subject,$this->_body);

}
}

In this example after define specific variables we have return $this. Because of that we can use Method Chaining, to construct a fluent code like this:

$email = new Email;
$email->to('juliopfneto@gmail.com')->from('joomla@noix.com.br')->subject('Usign FluentInterface')->body('body')->send();

The last method send() get all defined vars and call my specific class or thing with send mail. You dont need know. If you change order of methods works too.

$email->subject('Usign FluentInterface')->from('joomla@noix.com.br')->body('body')->to('juliopfneto@gmail.com')->send();

Know think on this for share data between applications, create a layer for developers use your services. You can make changes in your framework but not affect the layer.

This is a little introduction for my Fluent Application I will show some code I will not explain what framework do, try understand by your self.

$fluentApplication = new Fluent_Application();

//Creating instance of Joomla! Application
$site1 = $fluentApplication->getInstance('Joomla');
//configure connection
$site1->configureConnection()->host('localhost')->username('root')->password('')->database('joomla_subsite1')->prefix('jos_')->connect();

//Creating instance from another Joomla! Application
$site2 = $fluentApplication->getInstance('Joomla');
//configure connection
$site2->configureConnection()->host('localhost')->username('root')->password('')->database('joomla_subsite2')->prefix('jos_')->connect();

//get the "Content Domain" for work;
$content = $site1->domain('content');

/**
* Return Latest Content Published in Order
* use fluent interface for create Dinamyc
*
* Selectors Methods:
* - lists()
* - count()
*
* Database Methods:
* - getRows = $db->loadObjectList()
* - getResult = $db->loadResult();
*
* If you want know what methods have in domain you can use getMethods() *returns all methods in Array.
*/

echo 'Content Domain methods:';
echo '
  • '; foreach($content->getMethods() as $method){ echo '
  • '.$method.'
  • '; } echo '
';
/**
* Fluent Interface
*
* @var unknown_type
*/

$contentData = $content->lists()->latest()->limit(0,5)->getRows();

foreach($contentData as $row){
echo 'id.'"'>'.$row-'>title.'';
echo '
'; }

echo '
'; //is the samething $contentData = $content->limit(0,5)->lists()->latest()->getRows();
foreach($contentData as $row){
echo 'id.'"'>'.$row-'>title.'';
echo '
'; }

echo '
'; $content2 = $site2->domain('content'); $contentData2 = $content2->lists()->latest()->published()->inOrder()->limit(0,5)->getRows();
foreach($contentData2 as $row){
echo 'id.'"'>'.$row-'>title.'';
echo '
'; }

I will post more examples and create a plugins for install my framework soon.
See more in discussion
Torkil Johnsen Comment by Torkil Johnsen on October 28, 2009 at 5:28am
Yes, fluent interfaces are sweeeeeeet, and Nooku FW now provides it!

Check out this model example:
KFactory::get('admin::com.harbour.model.boats')->order('name')->limit(10)->offset(20)->enabled(1)->getList();

Copied from http://twitter.com/nooku/status/5217191387
Rodrigo Smania Spillere Comment by Rodrigo Smania Spillere on October 28, 2009 at 6:36am
Awesome project!

Congrats Júlio. I'm going to test your code!

Keep up the good work! You have a lot of talent.

Best,

Rodrigo Spillere

Comment

You need to be a member of All Together, As A Whole to add comments!

Join All Together, As A Whole

© 2010   Created by Robert Vining on Ning.   Create a Ning Network!

Badges  |  Report an Issue  |  Privacy  |  Terms of Service

Sign in to chat!