doushou1298 2015-03-19 10:24
浏览 64
已采纳

使用extract()将参数从控制器传递到php mvc模式中的视图

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');
        }

}

?>
  • 写回答

1条回答 默认 最新

  • douxi6903 2015-03-19 10:45
    关注

    extract() imports variables into the symbol table of the current scope. Therefore the "extracted" variables are available from within the render() method.

    PHP docs on extract:

    Import variables from an array into the current symbol table.

    The focus hereby is on the word current.


    The reason why the variables are available from within the file that is required is, that all language constructs regarding inclusion inherit the scope of the place from where the file was included.

    PHP docs state:

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.


    Variables you introduce in the included file will also only be available in the current scope. However functions and classes won't. They will reside in the global scope.


    Will the view share the same symbole table as the contoller?

    It will use the symbol table of the controllers render() method in your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改