tags:

views:

39

answers:

2

Example:

$this->getResponse()
     ->appendBody('Hello' . $name)

In the previous example, I understand the use of the first arrow operator, but not the second, since I don't know whether what the second one does is similar to passing arguments to the function, in which case I wonder why it doesn't go inside the parenthesis.

+5  A: 

I believe that second operator just calls appendBody() on the object returned by $this->getResponse().

In other words, it's a shortcut for this:

$x = $this->getResponse();
$x->appendBody('Hello' . $name);
djacobson
Also known as method chaining -> http://en.wikipedia.org/wiki/Method_chaining
Jacob
Thank you. I see it is a nice way to reduce code and improve readability... nice examples in the wikipedia link. Nice.
thebrotherofasis
+2  A: 

The same as a . in other OOP languages: You're chaining commands together.

You call $this->getResponse() which returns an object, then on that object you're calling appendBody(). it'd be the same as this:

$response = $this->getResponse();
$response->appendBody('Hello'.$name);

Ironically, I was just thinking about/playing with this about 10 minutes ago.

Slokun
Countering the downvote which I have no idea why was cast.
BoltClock
@BoltClock Thanks, I was wondering why, then realized I had some seriously messed up typos, which I subsequently fixed
Slokun