Blog Tutorial Question

I’ve been going through the blog tutorial and noticed when working in the Post model the tutorial says to add this function:

The page is here: http://www.yiiframework.com/doc/blog/post.model

The demo has the same code but for the life of me I can’t figure out where the variable

comes from? Is this a getter/setter thing? It obviously returns the array of status options but I have no idea where this variable was ever set. Any clues would be greatly appreciated.

Thanks.

Yes, implementing the getter you automagicly has the propertie!

Yii uses the PHP magic methods for this.

The blog doc is incomplete. It’s also written in extremely passive grammar, which is inappropriate for technical literature. I presume this is because it was written by a non-native speaker, so, as such, is forgiveable, but none the less, makes it very confusing and, therefore, frustrating at times.

When I got stuck with the tutorial, I did the same as you and went to the demo. It’s not identical, but for a determined reader, it’s okay.

Anyway, $this->statusOptions creates a call to $this->getStatusOptions():




public function getStatusOptions() {

  return array(

    self::STATUS_DRAFT => 'Draft',

    self::STATUS_PUBLISHED => 'Published',

    self::STATUS_ARCHIVED => 'Archived');

}



It does this via the so-called PHP “magic methods” __get and __set (see Overloading in the PHP manual). If you grep ‘__get’ you’ll find them all over. Take a look at CActiveRecord and it might help.

Thanks for the posts. I had an idea it was something to do with magic methods but definitely wanted to verify. I guess you could just blindly accept it but I don’t like “magic” going on without me at least understanding it a little.

Thanks.