Yii Framework Forum: Dynamic Class Attributes - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamic Class Attributes Add class attributes dynamically. Rate Topic: -----

#1 User is offline   yiisus 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 13-February 11

Posted 09 March 2011 - 04:40 PM

Hi,

Is there a reason why in yii I can not create dynamic class atributes.

This is what I get when instantiating A:

Property "A.dynamic" is not defined


Thanks and sorry, but I could't find an answer in the forums.

class A extends CModel
{
    public $var1;

    public function __construct()
    {
        $this->dynamic= 'test';
    }
}


0

#2 User is offline   Jaymard 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 08-March 11
  • Location:New Zealand

Posted 09 March 2011 - 07:54 PM

Hi yiisus,

You should declare $dynamic in the class.

class A extends CModel {
    public $var1;
    public $dynamic; //Declare dynamic as a class variable

    public function __construct()
    {
        $this->dynamic= 'test';
    }
}

0

#3 User is offline   Yoda Pop 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 11-August 10
  • Location:Ontario, Ca

Posted 09 March 2011 - 09:23 PM

The answer to your question can be found in the __set() magic method that is part of CComponent, which means all component subclasses are subject to this method of setting. The code does not allow for dynamic variables; as you can see, it throws an exception. For more information about the reasoning behind the method, read the CComponent documentation.
0

#4 User is offline   yiisus 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 13-February 11

Posted 10 March 2011 - 12:27 AM

View PostJaymard, on 09 March 2011 - 07:54 PM, said:


You should declare $dynamic in the class.

class A extends CModel {
    public $var1;
    public $dynamic; //Declare dynamic as a class variable

    public function __construct()
    {
        $this->dynamic= 'test';
    }
}


Thanks Jaymard. I just realized that I provided a bad example. But thanks to "Yoda Pop" and Calvaria in the other thread that you replied, I learned that I need to use magic functions (__set() & __get) to accomplish what I needed.

View PostYoda Pop, on 09 March 2011 - 09:23 PM, said:

The answer to your question can be found in the __set() magic method that is part of CComponent, which means all component subclasses are subject to this method of setting. The code does not allow for dynamic variables; as you can see, it throws an exception. For more information about the reasoning behind the method, read the CComponent documentation.


Thanks Master Yoda Pop :)
I definitely will go over the CComponent documentation. I have so many questions ..., but I'm moving on with my project.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users