I was trying to understand a php MVC tutorial and i couldn't understand how parameters were passed from the controller to a newly created view using the
extract();
(see the Controller class below) to add them in the symbol table (i don't know what will be its scope) inside a function render()
from the Controller class and then on that same function call the require($view)
function to display the view inside which these extracted variables will be simply available for call with a <?php echo $var; ?>
.
For me these extracted variables will be available only locally inside the function in which they were extracted ( it means the render()
function).
Is it because the require function was called in that same level that those extracted variables will be available inside the view ? Will the view share the same symbole table as the contoller? or will these variables be set to the global scope ?
<?php
class Controller{
public $request;
public $vars = array();
function __construct($request){
$this->request = $request;
}
public function render($view){
extract($this->vars);
$view = ROOT.DS.'view'.DS.$this->request->controller.DS.$view.'.php';
require($view);
}
public function set($key,$value=null){
if(is_array($key)){
$this->vars += $key;
}else{
$this->vars[$key] = $value;
}
}
}
?>
PagesController.php in which the render() function will be called :
<?php
class PagesController extends Controller{
function view($nom){
$this->set(array('phrase' => 'Salut ',
'nom' => 'Bohh')
);
$this->render('index2');
}
}
?>