For all my practice with OOP I have never used $this outside of the class definition.
While in zendframework we use $this in view template files, obviously it is not the scope of a class definition. I wonder how it has been implemented ? I have googled a lot but got no luck.
I want to know the mechanism how zendframework renders it's view files with $this.
It actually is in the scope of a class definition. Simple test-case:
Now, create another file:
Now go and visit view.php, and you will see that my-view.php had access to the private variable of the View class. By using
include
, you actually load the PHP file into the current scope.In view script files (
.phtml
ones)$this
refers to the currently used instance ofZend_View
class - the one that is ordered to render this particular script. Quoting the doc:And this it how it's done: when your controller calls (explicitly or implicitly)
render
method (defined inZend_View_Abstract
class), the following method (defined inZend_View
class) is executed in the end:... where
func_get_arg(0)
refers to the full filename (path + name) of the included script.