My question is rather simple, given:
class MyClass{
function a(){
echo "F.A ";
}
function b(){
echo "F.B ";
}
}
$c=new MyClass;
$c->a()->b()->b()->a();
So that it will output:
F.A F.B F.B F.A
What changes to code need to be made for this to work, or should it work as is or even just what this is called. If I could get whatever this term is called I could research it mysqlf, but I am not quite sure what to Google.
Thanks in Advance!
Stringing together methods like that is called “chaining.”
return $this;in each method will enable chainability, since it keeps passing the instance from one method to the other, maintaining the chain.You have to explicitly do this, since PHP functions will return
NULLby default.So, you just need 2 more lines.
Live Example
Take a look at this article by John Squibb for a further exploration of chainability in PHP.
You can do all sorts of stuff with chainability. Methods commonly involve arguments. Here’s an “argument chain”:
Live Example