Add dynamic varible to form/model in Yii

39 Views Asked by At

Please help, how i can add dynamic varible in Yii CFormModel?

private function magic($name, $value) {
    $this->$name = $value;
}

or

$form = new FormGenerate($attributes);
$form->temp = '1';

Show me Exception "Undetermined property" =(

2

There are 2 best solutions below

0
On

I create varible $_params;

And

public function __get($name)
    {
        if (isset($this->_params[$name])) {
            return $this->_params[$name];
        }

        return parent::__get($name);
    }


    public function __set($name, $value)
    {
        if (isset($this->_params[$name])) {
            $this->_params[$name] = $value;
        } else {
            parent::__set($name, $value);
        }
    }

It's work =)

0
On

In your class try something like this

class Test
{
    public function __construct($x)
    {
        $this->{$x} = "dynamic";
    }
}

$a = new Test("bar");
print $a->bar;

try changing your

$this->$name

to

$this->{$name}

to see what happens.