In Codeigniter, we can do :
$this->select('users')->orderBy('id')->limit(20)
I think this way of attaching methods to each other can work very good for me in my simple set of classes, but how to do it ?
This is called a fluent interface. To implement it, the function simply needs to return itself. Since the object is returned by reference, you can then chain together multiple calls:
class SomeClass
{
public function select($table)
{
// do stuff
return $this;
}
public function orderBy($order)
{
// do stuff
return $this;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With