setting public class property in Yii

In the controller (which is a class called MessageController), there is this code, which renders a “view” file named helloWorld and also sets an array where the variable $theTime is connected to the key ‘time’.




$theTime = date("D M j G:i:s T Y"); 

$this->render('helloWorld',array('time'=>$theTime));

In the view helloWorld file, the key ‘time’ from the controller is displayed here through the variable $time


<h3><?php echo $time; ?></h3>

this works perfectly. However, the book also suggests trying another way. It says

I haven’t been able to figure out how to do this. Anyone know how

Maybe that means using in the controller:


$this->time = date("D M j G:i:s T Y");

$this->render('helloWorld');

And in the view:


<h3><?php echo $this->time; ?></h3>

A view is rendered as part of CController, so in your example, any class properties and methods can be accessed inside a view using $this.




class MessageController extends CController

{

    protected $time;


    public function init()

    {

        $this->time = date("D M j G:i:s T Y");

    }


//.. rest of controller code



in the view…




echo $this->time;