Cannot assign return of function to public variable

I’ve tried searching on the forums before I posted this because it’s so basic that I thought for sure it would be on here, but I didn’t see anything.

Anyways, I’m following the book and near the end of Chapter 2, we are challenged to:

I did that, and now nothing is rendering AT ALL. (By the way, I’m developing on my local machine using MAMP Pro 1.9.4 for the Mac. I’m also running Snow Leopard.)

I thought it was something I did, maybe some kind of mistake, so I started again from a brand new webapp.

From the console I typed:


/path/to/YiiBase/framework/yiic webapp test

Then I enabling Gii by editing ./protected/config/main.php.

From Gii, I created a new controller with a Controller ID of "message"; I set Action IDs to "index helloWorld"; and I left Base Class and Code Template to be their default values.

Then I opened up ./protected/controllers/MessageController.php and found the following (minus the commented code):




<?php


class MessageController extends Controller

{

	public function actionHelloWorld()

	{

		$this->render('helloWorld');

	}


	public function actionIndex()

	{

		$this->render('index');

	}


	// Uncomment the following methods and override them if needed

	/*

	public function filters()

	{

		// return the filter configuration for this controller, e.g.:

		return array(

			'inlineFilterName',

			array(

				'class'=>'path.to.FilterClass',

				'propertyName'=>'propertyValue',

			),

		);

	}


	public function actions()

	{

		// return external action classes, e.g.:

		return array(

			'action1'=>'path.to.ActionClass',

			'action2'=>array(

				'class'=>'path.to.AnotherActionClass',

				'propertyName'=>'propertyValue',

			),

		);

	}

	*/

}



Standard, right? I opened up hxxp://localhost/test/index.php?r=message/helloworld in my browser and all works well.

Then I edit ./protected/controllers/MessageController.php and add a public variable:




class MessageController extends Controller

{

	public $textVar = "This is sample text.";

	

	public function actionHelloWorld()

	{

...



Note, I do not change anything in the view or in actionHelloWorld() in order to display it. I don’t care about displaying it at this point, I just want to see if my syntax is correct.

I reload the page in the browser and everything still works like it’s supposed to.

Then I add the following right underneath it:




class MessageController extends Controller

{

	public $textVar = "This is sample text.";

	public $dateVar = date('c');

	

	public function actionHelloWorld()

	{

...



I reload the page again in the browser and now nothing will show up at all. So I go and take a look at my PHP Log and here’s what I see:

So, since I haven’t changed any other files, does that mean there’s a rogue parenthesis somewhere in Yii 1.1.6?

Any help would be greatly appreciated.

Thanks in advance!

Not at all…

So let’s resume… if you add that last line… you get this error about the parenthesis… and look at your line… you have a parenthesis there… so

why do you think that the problem is in Yii?

OT and IMO… the picture you put as the avatar is just not appropriate for the forum as you stated on your profile that you are a male…

You can’t use date() to initialize the property.

You can use init() function for the initializaiton.




class MessageController extends Controller

{

  public $textVar = "This is sample text.";

  public $dateVar;


  public function init()

  {

    parent::init();

    $this->dateVar = date('c');

  }


  public function actionHelloWorld()

  {

...



Well, if you see, in my code, I have a starting AND ending parenthesis that match up. That’s the only reason why I thought it was in Yii.

Please check your PM’s.

Great! Thanks. I kinda figured it was me, but I wasn’t sure… that’s why I posted here.