Answer the question
In order to leave comments, you need to log in
How to call class methods sequentially?
I'm new to php, now I'm trying to make a micro framework, before that I already worked with Laravel and wrote this function call:
Route::get(...)->name(...);
And I can't figure out how to make a function call sequentially, that is:
$obj = new T();
$obj->firstfunc()->secondfunc()...;
class T {
public function firstfunc() {
echo 'hello ';
}
public function secondfunc() {
echo 'world ';
}
}
$obj = new T();
$obj->firstfunc()->secondfunc();
Answer the question
In order to leave comments, you need to log in
For chaining, each method must return $this.
class Foo
{
public function firstfunc()
{
echo 'hello ';
return $this;
}
public function secondfunc()
{
echo 'world ';
return $this;
}
}
$obj = new Foo();
$obj->firstfunc()->secondfunc();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question