Fluent interface and method chaining in PHP and JavaScript
Tags: programming, fluentinterface, javascript, php
Published: 2018-08-15 05:52:23

---
title: Fluent interface and method chaining in PHP and JavaScript
published: true
description: 
tags: programming, fluent interface, javascript, php
---

A Fluent Interface is an object oriented API that provides "more readable" code.
A fluent interface allows you to chain method calls, which results in less typed characters when applying multiple operations on the same object.

[read more about fluent interface](https://en.wikipedia.org/wiki/Fluent_interface)

Fluent interface let us write code like this:
```php
$programmer->born()->eat()->sleep()->code()->die();
```
instead of 
```php
$programmer->born();
$programmer->eat();
$programmer->sleep();
$programmer->code();
$programmer->die();
```

Implementation:
A fluent interface is normally implemented by using method chaining to implement method cascading (in languages that do not natively support cascading), concretely by having each method return this (self). Stated more abstractly, a fluent interface relays the instruction context of a subsequent call in method chaining, where 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.

[from here](https://en.wikipedia.org/wiki/Fluent_interface)


A simple PHP example 

```php
class Person{
  private $_name;
  private $_sex;
  private $_age;
  private $_height;
  private $_weight;

  public function name($name){
    $this->_name = $name;
    return $this;
  }

  public function sex($sex){
    $this->_sex = $sex;
    return $this;
  }

  public function age($age){
    $this->_age = $age;
    return $this;
  }

  public function height($h){
    $this->_height = $h;
    return $this;
  }

  public function weight($w){
    $this->_weight = $w;
    return $this;
  }
  public function save(){
    $properties = get_object_vars($this);
    $str = '';
    foreach($properties as $property){
        $str .= $property.' ';
    }
    return $str;
  }
}
```
Now we can call the methods on the object of Person like 

```php
$p = new Person();
$res = $p->name('Sonia')->sex('Female')->age('30')->height('5.8')->weight('51')->save();
echo $res; # Sonia Female 30 5.8 51
```
we can call only few of them 

```php
$res = $p->name('Sonia')->sex('Female')->age('30')->save();
echo $res; # Sonia Female 30
```
we also can call chain methods in deffrent order

```php
$res = $p->name('Sonia')->height('5.8')->weight('51')->sex('Female')->age('30')->save();
echo $res; # Sonia Female 30 5.8 51
```
Same in JavaScript
```javascript
var Person = function() {
  this._name = '';
  this._sex = '';
  this._age = '';
  this._height = '';
  this._weight = '';
};

Person.prototype.name = function(name) {
  this._name = name;
  return this;
};

Person.prototype.sex = function(sex) {
  this._sex = sex;
  return this;
};

Person.prototype.age = function(age) {
  this._age = age;
  return this;
};

Person.prototype.height = function(height) {
  this._height = height;
  return this;
};

Person.prototype.weight = function(weight) {
  this._weight = weight;
  return this;
};


Person.prototype.save = function() { 
  let str = '';
  for(let property of Object.keys(this)){
    str += this[property] + ' ';
  } 
  console.log(str);
};
```
Using it
```javascript
new Person().name('Sonia').sex('Female').age('30').height('5.8').weight('51').save();
// Sonia Female 30 5.8 51
```