drylko, on 01 April 2013 - 07:07 AM, said:
Check CWebUser::__set() and CWebUser::__get() and ["www.php.net/manual/en/language.oop5.overloading.php"]this]
Thanks for pointing me in the right direction. This really was helpful. Below, I'll try to clarify what I discovered following those links (especially the link on PHP: Overloading from the PHP manual).
This is probably very fundamental stuff in PHP, but I'll attempt to list the reasons why one (the Yii Framework specifically) would use "dynamically created properties" vs. explicitly defined properties with getters and setters. If we can understand why, it helps in learning and using the language and framework more effectively.
I am brand new to PHP on YII, so I'm hoping someone will fill in the gaps and correct the mistakes here.
Reasons to use Dynamically Created Properties and Methods (what PHP calls "Overloading" but from a traditional sense it's actually overriding (or interpreter hooks)):
You can make the property READ ONLY and still allow access to the property with ->, rather than with a Getter.
You can pass properties via an array, rather than having to use individual arguments for each setting. PHP doesn't support named parameters, so the only way to pass the initial values to an object's constructor would be to specify values for all items (the ordering is important).
This might be why they call this Overloading. It is a technique that allows for an overloading(in the traditional sense) workaround, in addition to named parameters.
Reasons not to do this: (This is directly copied from a post by theaceofthespade at gmail dot com in PHP: Overloading from the PHP manual, but he articulates my sentiments as well or better than I could have.)
A word of warning! It may seem obvious, but remember, when deciding whether to use __get, __set, and __call as a way to access the data in your class (as opposed to hard-coding getters and setters), keep in mind that this will prevent any sort of autocomplete, highlighting, or documentation that your ide mite do.
Furthermore, it beyond personal preference when working with other people. Even without an ide, it can be much easier to go through and look at hard coded member and method definitions in code, than having to sift through code and piece together the method/member names that are assembled in __get and __set.
If you still decide to use __get and __set for everything in your class, be sure to include detailed comments and documenting, so that the people you are working with (or the people who inherit the code from you at a later date) don't have to waste time interpreting your code just to be able to use it.
I'm still curious how dynamically created properties can be preferable to the alternative. But, maybe this is just something I need to accept and that I'll get used to. Or, maybe it's just a style or preference thing.