Php Reflections In Yii

Hello guys

So, I need to set the value of a property (of a CActiveRecord model) just knowing its name, and in a first place, I thought that using reflections was the most simple method, cause with two lines is sufficient.

But, to my surprise is not possible cause a property of CActiveRecord model cannot being “set” (try it using the method canSetProperty()). :huh:

Following, the code that I use to set a property, but that as property is not accesible, it’s unseless.

$reflectionClass = new ReflectionClass('Questioner');





Why the property is not accesible?[/size]

Many Thanks


AR attributes can be set directly ($model->attribute = $value) but are not object properties. This is done using magic __set method so attributes can be treat as properties for convenience and you don’t have to use Reflection. Attributes are stored in $_attributes array.

Easy cheese!

It Worked!

When can you use? Magic methods are fine when just it is going to know the name of the property at runtime.

class newClass{



$object = newClass();

$property = 'name';

$value = 'vivaPHP';

$object->$property = $value; // here PHP throught the __set magic method recognizes the property with this name.