Ah sorry, I missed you were looking for an example on this.
It works from left to right. Here is a quick example, its sorta infeasable but should show you what it does:
PHP Code:
class Foobar
{
public function function1()
{
printf("%s\n", __METHOD__);
return $this;
}
public function function2()
{
printf("%s\n", __METHOD__);
return $this;
}
public function function3()
{
printf("%s\n", __METHOD__);
return $this;
}
}
$fb = new Foobar();
$fb->function1()->function2()->function3();
So, calling $fb->function1(); returns a Foobar instance. That result can then be chained to immediately call function2() which also returns a Foobar instance, and then function3() would be called.
This is generally not done on a single object, but may be done on properties within one. For a better example:
PHP Code:
class Foobar
{
private $db;
public function __construct(DatabaseClassOfSomeKind $db)
{
$this->db = $db;
}
// This one will return the DB stored, which according to the constructor is
// a type of DatabaseClassOfSomeKind. We can then immediately use it.
public function getDB()
{
return $this->db;
}
}
$fb = new Foobar(new Database());
// These two are similar, except the assignment takes more memory:
$db = $fb->getDB();
$db->connect();
// And
$fb->getDB()->connect();
So, from you're last question, each method is required to return an object to be worked on. As soon as you see a -> in PHP, it means you're accessing either a member property or a method on an object (which also means that the previous method call in this example returned an object of some type).
Hope that helps!
Edit:
I want to add as well, that PHP struggles on a similar context with static methods and members. It will likely throw a T_PAAMAYIM_NEKUDOTAYIM error at you.