I have been pouring over Zend_View and the various classes and interfaces that make up the view. One thing I am attempting to replicate in a project that does not use zend in any way shape or form is the:
$this->view->variable = 'Hello world';
that you can set in a controller and then do:
echo $this->view->variable;
My ultimate goal is to do something like:
$this->variable = new SomeClass
and then else where, in a view specifically, do:
$this->variable->someMethod();
My question is:
- How would I replicate what zend does to do something simmilar with out using global variables?
- How is zend able to do something like $this->view with out ever instantiating or saying what view is?
this would help me understand how, variables are passed around or objects are passed from the logic to the view and how php allows for something like $this->view to work when in a view or not.
note: this is not a Zend specific question and “use zend” is not the answer. I am looking to replicate a specific feature. My project does not in any way use or affiliate with zend.
It’s actually pretty simple. When you use
include,require,evalet al., the loaded code is brought in to the current scope. So if you have a template filetemplate.php
Controller.php
index.php
Blamo.., template.php is able to call
$this->view->somevaras it is treated as part of Controller.php. Runningphp index.phpon the CLI producesTo elaborate a tiny bit, if
$this->viewinside of Controller.php were a class you’ve defined rather than a simple instance ofstdClassas in the above demonstration, and it had a functionsomeMethod, you could call$this->view->someMethod()from template.php just the same.