Hopefully an easy question about model attributes

So I’ve been looking at the code trying to figure out how things work as I am just getting started with Yii version 1.1. The code I was looking at was the default code that is setup right after you initially set up Yii. Specifically I was playing around with the login page and was trying to figure out where the $attributes class variable is at that’s used in the SiteController.php file. This was how I went about looking at things:

SiteController.php:

In actionLogin() I see $model=new LoginForm and then later I see $model->attributes=$_POST[‘LoginForm’]. Seems simple enough, so on to my quest of finding the actual $attributes class variable. I went to look at LoginForm.php.

LoginForm.php:

I don’t see any variable called $attributes. No biggie, I’ll backtrack some more as this class extends CFormModel.

CFormMoel.php:

No variable called $attributes here either. But this extends another class called CModel, maybe it’s there.

CModel.php:

Nope, don’t see it here either. This extends CComponent, so let’s check there.

CComponent.php

Well it’s not here either and this class doesn’t extend any others so we’re at the top of the chain.

So anyway, you get my drift. Since I’ve already tried out the login page I know that the statement $model->attributes=$_POST[‘LoginForm’]; does work, I just don’t understand how it does when there doesn’t seem to be any parent class that contains an $attributes variable when following the LoginForm’s inheritance chain.

I’d appreciate any info, dumbed down or otherwise. Thanks in advance.

It’s a public property defined by CModel.

http://www.yiiframework.com/doc/api/CModel

/Tommy

Please also read the following guide careful. It forms the foundation of component-based programming:

http://www.yiiframework.com/doc/guide/basics.component

Thanks for your replies guys. After reading your posts and and looking at your links, I had a better idea of what to search for online. I found that the __get and __set methods you are using are known as magic functions with php 5. Being that I was only familiar with php 4 this was definitely a new concept for me. It took me a little while but I finally found a good article online that broke it down in a way that made sense to me.

So in case there are any others that find __get and __set a little confusing and hard to follow, head on over to devshed.com and look for an article called Magic Functions in PHP 5. It’s explained pretty well with an easy example using the old getters and setters methods and the new __get and __set methods. The article also mentions some pros and cons to using __get and __set versus the traditional getters and setters.

Once again, thanks for your replies.