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.

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

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂