【发布时间】:2013-08-13 10:00:56
【问题描述】:
不久前我开始使用 Zend Framework 2。
在控制器中,我发送变量以查看使用
return $viewmodel->setVariables(array(
'exampleVariable' => 'exampleValue',
'exampleVariable2' => array(
'variableInArray' => $this->getMacAddress(),
),
));
在我正在做的视图中:
$exampleVariable = $this->exampleVariable
// and
$exampleVariable2 = $this->exampleVariable2
然后直接使用这些变量,这样我每次使用它们时都不必经过$this。
我正在研究它,并修改了一些东西,当我想调试时,我删除了前两行,希望它会中断。
令我惊讶的是,$exampleVariable 和 $exampleVariable2 仍然可用。一开始以为是缓存问题,结果发现所有用SetVariables()发送到视图的数组键都可以作为变量访问。
我的问题是,没有$this,怎么可能访问它们?
我可能会收到警告,但这个问题只是出于好奇。我不会直接使用变量,因为我更喜欢在视图中创建它们,这样我就可以评论它们并添加它们各自的变量类型和内容。
【问题讨论】:
标签: php templates zend-framework2 scope