C5-8.2.1: Why string doesn't get set with value?

Permalink
[UPDATED]
If I declare a string in a class and set its value right there, everything works.

But if I only declare a string in a class, but set its value in the view(), the value doesn't get set, it returns empty strings. Why?

This works:
class Controller extends BlockController
{
    public $name = 'Name';
    public function view() 
    {
        $this->validate();
    }
    public validate() {
         $this->set('name', $this->name);
    }
}

This does not:
class Controller extends BlockController
{
    public $name = '';
    public function view() 
    {
        $this->name = t('Name');
        $this->validate();
    }
    public validate() {
         $this->set('name', $this->name);
    }
}

How can I set variables in the controller so that they can be both used in the view.php and also get translated? The t() function obviously doesn't work on variable declarations in a class.

Thank you.

linuxoid
 
hutman replied on at Permalink Reply
hutman
You need to do it like this

class Controller extends BlockController
{
    public $name = '';
    public function view() 
    {
        $this->name = t('Name');
linuxoid replied on at Permalink Reply
linuxoid
Hi.

Sorry, I made a mistake when I was writing it for the post. I've updated the original code above.
linuxoid replied on at Permalink Reply
linuxoid
I also tried setting the property in a constructor - still an empty string returns.

The dilemma is I can either set public string properties in the class but how can I have them translated? Or I can use local variables in the view() - this works fine, but I can't use them throughout other methods.

How can I set public string properties and have them picked up by the t()?
linuxoid replied on at Permalink Reply
linuxoid
Ok, I got it. I had to put my property assignments in the on_start() and that's it, it all works now.