开发者

in oop php what does using an arrow operator -> after a method do?

开发者 https://www.devze.com 2023-01-18 19:09 出处:网络
Example: $this->getResponse() ->appendBody(\'Hello\' . $name) In the previous example, I un开发者_开发知识库derstand the use of the first arrow operator, but not the second, since I don\'t kn

Example:

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

In the previous example, I un开发者_开发知识库derstand 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.


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消