Difference between #5 and #6 of
Understanding Virtual Attributes and get/set methods

Revision #6 has been created by Steve Friedl on Apr 1, 2011, 2:40:42 PM with the memo:

Added a section about Dynamic Attributes
« previous (#5) next (#7) »

Changes

Title unchanged

Understanding Virtual Attributes and get/set methods

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Virtual Attributes; __get; __set

Content changed

[...]
Yii has an intricate system of housekeeping that supports almost anything you wish to accomplish on your own - especially Virtual Attributes - and attempting to circumvent this may provide more smoke than light. It will almost certainly make your application harder to understand.

If you **must** override the magic methods in your own code, be sure to call `parent::___get($attr)` (et al) to give Yii a crack at the attributes in case your code doesn't handle it.

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: 202 618 times
Version: 1.1
Category: Tutorials
Written by: Steve Friedl
Last updated by: GOsha
Created on: Mar 31, 2011
Last updated: 12 years ago
Update Article

Revisions

View all history