I was going through some tutorial and documentation about zend framework, most of things made sense until i came across $this variable in /application/layout/scripts/layout.phtml, it was mentioned that $this is an instance of the view object that was created during bootstrapping.
to my knowledge you cannot use $this as the variable name as because $this is a reserved keyword for php used to refer the same object within the class context. any attempt to use it as a variable will result in Fatal error with the following error message Fatal error: Cannot re-assign $this and as per the author’s statement There is a variable, $this, available which is an instance of the view object, i am unable to understand the theory behind this. how come $this is being used out of the class context?
It’s actually being used in the context of an object. You should look at the code yourself, but the basic idea behind render() (which is the toString method by proxy):
Zend Framework does it a bit more complexly so that it’s a bit more flexible than that, but it’s the basic idea.
Then, inside of the viewScript, it’s technically inside of the render() method just as if the code were literally in that “include …” place. (Oversimplifying that, but the general idea holds.)