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
-
- namespace DesignPatterns\Structural\FluentInterface\Tests;
-
- use DesignPatterns\Structural\FluentInterface\Sql;
- use PHPUnit\Framework\TestCase;
-
- class FluentInterfaceTest extends TestCase
- {
- {
- $query = (new Sql())
- ->select(['foo', 'bar'])
- ->from('foobar', 'f')
- ->where('f.bar = ?');
-
- $this->assertEquals('SELECT foo, bar FROM foobar AS f WHERE f.bar = ?', (string) $query);
- }