在控制器(它是一种称为MessageController类),有该代码,这使得一个“视图”的文件叫做helloWorld并且还设置其中变量$ theTime连接到所述阵列关键'时间'。设置公共类属性
$theTime = date("D M j G:i:s T Y");
$this->render('helloWorld',array('time'=>$theTime));
在视图helloWorld的文件,显示在这里通过变量$时间
<h3><?php echo $time; ?></h3>
这工作完全来自控制器的关键“时间”。但是,这本书也建议尝试另一种方式。它说
Alter the previous example by defining a public class property on MessageController, rather than a locally scoped variable, whose value is the current date and time. Then display the time in the view file by accessing this class property through $this.
我一直无法弄清楚如何做到这一点。任何人都知道如何
我不认为你可以有一个表达式评估这样的变量,但我尝试了你的建议,并且我得到了这个错误 解析错误:语法错误,意外的'(',期待','或'; “在/Applications/MAMP/htdocs/demo/protected/controllers/MessageController.php第6行 – Leahcim 2011-05-27 09:42:14
我搞砸与代码,而现在编辑。没有在'Yii'代码为过长...的时间,我想你最好把'$这 - >时间=日期( “DMĴG:我:■TY”);''中beforeAction($动作)'方法 – Nemoden 2011-05-27 09:44:30
@Nemoden此(你张贴第一方式)不是Yii的特异性。你不能默认值分配给属性,如果它不是一个标量。[看更多的特性在PHP 5](http://php.net/manual/en/language.oop5.properties.php)。但现在它是确定,如果'beforeAction()被执行' – Tadeck 2011-05-27 09:59:05