The "arrows" refer to an instance method (the functions that an object can invoke when it is instantiated), when applying one after another each method invoked should return an object, with an example it may be understood more:
class Foo() {
public function devolverNumeroAlCuadrado($num) {
return $num * $num;
}
public function otroMetodo($num) {
echo "Cualquier cosa";
}
}
$var= new Foo(); // Instancio el objeto
$var->devolverNumeroAlCuadrado(2)->otroMetodo(); // ERROR!
Throw error since as devolverNumeroAlCuadrado()
returns a number, when calling otroMetodo()
we are saying something like this:
4->otroMetodo();
And number 4 does not have that method. Therefore, if we want to call otroMetodo()
what the previous methods return will have to be the same instance:
class Foo() {
public function unMetodo() {
// Hace algo
return $this; // Retorna el objeto llamador
}
public function otroMetodo($num) {
echo "Cualquier cosa";
}
}
$obj1 = new Foo(); // Instancio el objeto
$obj1->unMetodo()->otroMetodo(); // AHORA SI!!!
Another example using two different classes:
class Foo() {
public function unMetodo() {
// Hace algo
return $this; // Retorna el objeto llamador
}
public function otroMetodo($num) {
return new Bar();
}
}
class Bar() {
public function metodoSalvaje() {
echo "Soy un Bar!";
}
}
$obj1 = new Foo(); // Instancio el objeto
$obj1->unMetodo()->otroMetodo()->metodoSalvaje();
I hope I have been clear and useful!
Greetings!