Fluent Interfaces

A term first introduced by Martin Fowler and Eric Evans, a fluent interface is one in which a class is self-referencial wherever possible, resulting in cleaner code. Wherever possible, the Istarel Workshop Application Framework utilizes fluent interfaces.

Making your own code fluent can be astonishingly easy: simply return the current object in any method where you are modifying the state or behavior of the object.

Partial Listing: /bnr/rsrc/model/default/DefaultStudent.php

class DefaultStudent extends ORMObject
{
    ...

    function setFirstName($first_name)
    {
        $this->setValueForProperty('first_name', $first_name);
        return $this;
    }

    ...
}

Using that technique, I can now write very elegant code. Notice that in the example below, I made a design choice to visually separate the save() method.

Partial Listing: /path/to/imaginary/script

$student = new Student;
$student->setFirstName('Mark')
        ->setLastName('Fenoglio')
        ->setCompany('Istarel Workshop');
$student->save();