Sometime I find some way of OOP programming that $this keyword is used to point the current method and which also point another method. i.e. $this->getView()->render($view, $params, $this)
the live in github. Another example, $this->view->setVar("postId", $postId);
. I do not know what it is called and how to use it.
Asked
Active
Viewed 21 times
0

StreetCoder
- 9,871
- 9
- 44
- 62
1 Answers
1
It's called a "fluent interface".
$this
still only "points" to a single method or property, but that method can return (or a property can hold) a value of any PHP datatype, which includes other objects.
In the first example, the getView()
method returns an object, which in turn has a method called render()
; while in the second case, the view
property holds an object which has a setvar()
method.

Mark Baker
- 209,507
- 32
- 346
- 385
-
Thanks @Mark. I am new in this concept. Let me google. However, if you good reference to learn that would be helped. – StreetCoder Feb 22 '15 at 11:44
-
http://en.wikipedia.org/wiki/Fluent_interface#PHP – Mark Baker Feb 22 '15 at 11:45
-
http://devzone.zend.com/777/fluent-interfaces-in-php/ – Mark Baker Feb 22 '15 at 11:47
-
do you want to say that `getView()` method returns an on object which should have the method `render()`, right? But I did not find object `getView()` had the method `render()`. – StreetCoder Feb 22 '15 at 14:29
-
What object is returned by the `getView()` method, and what methods does that object have? – Mark Baker Feb 22 '15 at 15:53
-
It returns a lot of methods but I did not find `render()`. However, it is core file of Yii2. Not much important to know this things. Just to be confirmed about my understanding that object should have method in this situation, right? `$this->obj()->method()`. – StreetCoder Feb 22 '15 at 16:00
-
If the class extends a core Yii class, then that might implement the render() method..... because otherwise it would give a runtime error – Mark Baker Feb 22 '15 at 16:02