unclear about Chapter 6, page 135

Although I’ve done reading about OOP, this book is the first time I’ve made anything with it, so I’m a bit confused.

This code is from page 135 of the book, where we are replacing the status id with status text to make the issue tracker more user friendly. The code is inserted into Class Issue (issue.php), which extends CActiveRecord

I believe


statusOptions

is a method (or could it also be a property?), but I can’t find where it’s being called from. In this context, would


$this

refer to the Issue class, or CActiveRecord? In either case, I can’t find a statusOptions in either, so I’m wondering where it’s coming from.

Thanks if you can help


public function getStatusText()

	{

		$statusOptions=$this->statusOptions;

		return isset($statusOptions[$this->status_id]) ? $statusOptions[$this->status_id] : "unknown status ({$this->status_id})";

	}



$this, in this case, refers to an instance of Issue class


$this->statusOptions

is a property. If it were a method, it would have parentheses, e.g.


$this->statusOptions();

So “statusOptions” should be a property of the Issue class, but when I look through Issue.php, i don’t see a property


statusOptions

. What am I doing wrong?

This function is supposed to be accessing statusOptions property in Issue class, but no properties listed, yet my application works. Where is it getting the statusOptions from? It’s trying to access the data in statusOptions but if there’s no statusOptions then…?


public function getStatusText()

	{

		$statusOptions=$this->statusOptions;

		return isset($statusOptions[$this->status_id]) ? $statusOptions[$this->status_id] : "unknown status ({$this->status_id})";

	}

Similar to my post right above, in protected/views/issue/admin.php where they are creating the menu, the book source code gives


array('label'=>'List Issue', 'url'=>array('index', 'pid'=>$this->getProject()->id)),

so $this is an instance of issue class, but in issue.php there is no getProject() method. Where is it?

Am I missing something?

Hello mjmitche,

Yes, it seems you are missing something.


$this

within a view file, like the one you have a question about (protected/views/issue/admin.php) refers to the controller class which is rendering the view. In this case, that would be the protected/controlers/IssueController.php class file.

And, if you look in this file, there is, indeed, a getProject() method.

thanks JeffTulsa,

My original question was about this code in issue.php, where $this refers to an instance of the Issue class, so I’m assuming that the property


statusOptions 

should be in issue.php but it’s not there?

If you have a moment, can you please clarify that. Thank you Cheers.


/**

	 * @return string the status text display for the current issue

	 */ 

	public function getStatusText()

	{

		$statusOptions=$this->statusOptions;

		return isset($statusOptions[$this->status_id]) ? $statusOptions[$this->status_id] : "unknown status ({$this->status_id})";

	}

With regard to your original question, Yii component classes make use of some of PHP’s magic methods in order to allow you to access getter and setter methods as if they were basic properties of the object itself. So, in this case there is a getter method defined in the model classed called


getStatusOptions()

And you can reference this either as


$issue->getStatusOptions();

or more simply as


$issue->statusOptions;

both are returning the result of the getStatusOptions() method within the Issue model class.

o.k. thank you so much.