Difference between #2 and #8 of
Understanding Virtual Attributes and get/set methods

Changes

Title unchanged

Understanding Virtual Attributes and get/set methods

Category unchanged

Tutorials

Yii version unchanged

Tags changed

Virtual Attributes;, __get; __set, understanding

Content changed

[...]
```php
class Comment extends CActiveRecord {
public $helperVariable;
public function rules() { ... }
...
}
 
```
and then use them in the obvious way:
[...]
lastname VARCHAR(32),
...
)
 
~~~
Yii's Active Record maps these easily into the `Person` model, which allows you to reference and assign `$model->firstname` and `$model->lastname` attributes anywhere in your code. ActiveRecord is one of the coolest feature of Yii.
[...]
```php
class Person extends CActiveRecord {

 
public function getFullName() { return $this->firstname . " " . $this->lastname; } ... }
 
```
With this getter function defined, `$model->fullname` automatically calls the function and returns the value as if it were a real attribute: this is a **virtual** attribute, and it's very powerful.
[...]
```php
// in a view somewhere

 
  
echo $form->dropDownList($model, 'personid',    CHtml::listData( Person::model()->findAll(), 'id', 'fullname' )   );
```
Now, the dropdown will show full user names in the dropdown, which makes for a better user experience. Attempting to provide a dropdown including firstname + lastname without a model helper function like this is more work and less useful.
[...]
```php
$x = $model->fullname;
    $x = $model->getFullname(); // same thing     $model->active = 1;     $model->setActive(1); // same thing ``` Note that Yii uses get/set functions **very** heavily internally, so when reviewing the class refernences, any function beginning with "set" or "get" can generally be used as an attribute.>

Resolving Conflicts
[...]
Please treat these methods as highly advanced, only to be used with good reason and careful consideration.

 
PHP Dynamic Attributes Don't Work
 
---------------------------------
 
More advanced PHP developers might wonder how Dynamic Attributes play into Yii, and the short answer is that they do not.
 
 
Dynamic Attributes allow an object variable to receive new attributes just by the using: saying `$object->foo = 1` automatically adds the attribute "foo" to the object without having to resort to `__get` or `__set`, and it's reported to be much faster.
 
 
But because of Yii's implementation of `__get/set`, these will not work because the low-level methods in `CComponent` throw an exception for an unknown attribute rather than let it fall through (which would enable dynamic attributes).
 
 
Some question the wisdom of blocking this, though others may well appreciate the safety it provides by insuring that a typo in an attribute name won't silently do the wrong thing rather than attempt to assign a close-but-not-quite attribute name to an object.
 
 
More info: [Dynamic Properties in PHP](http://krisjordan.com/dynamic-properties-in-php-with-stdclass)
66 0
44 followers
Viewed: 205 710 times
Version: 1.1
Category: Tutorials
Written by: Steve Friedl
Last updated by: GOsha
Created on: Mar 31, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history