2.8. Fluent Interface

    • Doctrine2’s QueryBuilder works something like that example classbelow
    • PHPUnit uses fluent interfaces to build mock objects
    • Yii Framework: CDbCommand and CActiveRecord use this pattern, too
    1.  
    2. namespace DesignPatterns\Structural\FluentInterface\Tests;
    3.  
    4. use DesignPatterns\Structural\FluentInterface\Sql;
    5. use PHPUnit\Framework\TestCase;
    6.  
    7. class FluentInterfaceTest extends TestCase
    8. {
    9. {
    10. $query = (new Sql())
    11. ->select(['foo', 'bar'])
    12. ->from('foobar', 'f')
    13. ->where('f.bar = ?');
    14.  
    15. $this->assertEquals('SELECT foo, bar FROM foobar AS f WHERE f.bar = ?', (string) $query);
    16. }