I’m working through a classic “Hello World!” tutorial, and here’s my problem:
One exercise had me altering the relevant controller action (originally containing simply:
public function actionHelloWorld()
{
$this->render(‘helloWorld’);
}
to:
public function actionHelloWorld()
{
$theTime = date("D M j G:i:s T Y");
$this->render(‘helloWorld’,array(‘time’=>$theTime));
}
The attribute $time is then available for me to echo out in my view. No problems there. I was then encouraged to, on my own, create a public class property for the current time in the controller which would then be accessible in the view via $this-> …
Seemed straightforward to me, but whenever I tried the following code "public $time = date("D M j G:i:s T Y");" in my controller class I get the following error before I ever get a chance to call anything from within the view:
“Parse error: syntax error, unexpected ‘(’, expecting ‘,’ or ‘;’ in C:\wamp\www\demo\protected\controllers\MessageController.php on line 6”
(line 6 being the line containing the above property code.)
The strange thing, is that if I put that code into a method, e.g.:
public function currentTime() {
return $theTime = date("D M j G:i:s T Y");
}
and then call it in my view via "<?php echo $this->currentTime(); ?>" then everything works fine. What in the world am I missing?