Search code examples
phpapplication-poolmethod-chaining

use a function after a function PHP OOP


i have a simple question in OOP

I've see in a lot of frameworks like Laravel something like this :

$data = Model::OrderBy('id','desc')->skip(5)->take(10)->get()->toArray();

My Question is, how can i can call a function after another function ?

Exemple :

class test{

public function test1(){}
public function test2(){}

}

how i can call the function test 2 after test 1 like that test1()->test2()

i hope the question is clear


Solution

  • In its simplest form you just have to return the current instance in all your chainable methods:

    return this;
    

    Often these chained methods return a new object instead of altering the current one though - this is called immutability or immutable objects.