freedom55 Posted June 18, 2011 Share Posted June 18, 2011 I am following a tutorial and have some questions about OOP and magic. I've got the following unit test: /unit/IssueTest.php public function testGetTypes() { $options = Issue::model()->TypeOptions; $this->assertTrue(is_array($options)); $this->assertTrue(3 == count($options)); $this->assertTrue(in_array('Bug', $options)); $this->assertTrue(in_array('Feature', $options)); $this->assertTrue(in_array('Task', $options)); } I don't understand how in OOP the function getTypeOptions() in model/Issue.php is called from /unit/IssueTest: model/Issue.php public function getTypeOptions() { return array( self::TYPE_BUG=>'Bug', self::TYPE_FEATURE=>'Feature', self::TYPE_TASK=>'Task', ); } In /unit/IssueTest.php we run this command: $options = Issue::model()->TypeOptions; I don't see a property called "TypeOptions" in the model?? How does the above command get the array of bug, features, task? Also, why does the model function have to be called? I read that a static property would be found like so: $options = Issue::TypeOptions; Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/ Share on other sites More sharing options...
Adam Posted June 18, 2011 Share Posted June 18, 2011 It's impossible to say exactly what's going on without seeing all of the code, but the model() method returns a different object containing the "TypeOptions" property. As the method returns an object, you can 'chain' the call to the property on the end. The code is effectively the same as: $options_object = Issue::model(); $options = $options_object->TypeOptions; Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231312 Share on other sites More sharing options...
ignace Posted June 18, 2011 Share Posted June 18, 2011 Like Adam already said ::model() returns a different object. This object in turn either has a property TypeOptions public $TypeOptions or a __set method. Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231372 Share on other sites More sharing options...
freedom55 Posted June 18, 2011 Author Share Posted June 18, 2011 Here is the class for Issue. I think I understand what you guys are saying, just having a tough time actually seeing it in the code. I don't see the property being set anywhere and think that the framework I'm using (yii) using using magic somehow to set the property. Just trying to understand how it works. It looks to me that "getTypeOptions()" is setting the properties. But they are getting called via "Issue::model()->TypeOptions;" Just trying to figure out how that works with dropping the "get" part. <?php /** * This is the model class for table "tbl_issue". * * The followings are the available columns in table 'tbl_issue': * @property integer $id * @property string $name * @property string $description * @property integer $project_id * @property integer $type_id * @property integer $status_id * @property integer $owner_id * @property integer $requester_id * @property string $create_time * @property integer $create_user_id * @property string $update_time * @property integer $update_user_id * * The followings are the available model relations: * @property User $owner * @property Project $project * @property User $requester */ class Issue extends CActiveRecord { const TYPE_BUG=0; const TYPE_FEATURE=1; const TYPE_TASK=2; /** * @return array issue type names indexed by type IDs */ public function getTypeOptions() { return array( self::TYPE_BUG=>'Bug', self::TYPE_FEATURE=>'Feature', self::TYPE_TASK=>'Task', ); } /** * Returns the static model of the specified AR class. * @return Issue the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'tbl_issue'; } /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('name', 'required'), array('project_id, type_id, status_id, owner_id, requester_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=>true), array('name', 'length', 'max'=>256), array('description', 'length', 'max'=>2000), array('create_time, update_time', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, name, description, project_id, type_id, status_id, owner_id, requester_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'owner' => array(self::BELONGS_TO, 'User', 'owner_id'), 'project' => array(self::BELONGS_TO, 'Project', 'project_id'), 'requester' => array(self::BELONGS_TO, 'User', 'requester_id'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id' => 'ID', 'name' => 'Name', 'description' => 'Description', 'project_id' => 'Project', 'type_id' => 'Type', 'status_id' => 'Status', 'owner_id' => 'Owner', 'requester_id' => 'Requester', 'create_time' => 'Create Time', 'create_user_id' => 'Create User', 'update_time' => 'Update Time', 'update_user_id' => 'Update User', ); } /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; $criteria->compare('id',$this->id); $criteria->compare('name',$this->name,true); $criteria->compare('description',$this->description,true); $criteria->compare('project_id',$this->project_id); $criteria->compare('type_id',$this->type_id); $criteria->compare('status_id',$this->status_id); $criteria->compare('owner_id',$this->owner_id); $criteria->compare('requester_id',$this->requester_id); $criteria->compare('create_time',$this->create_time,true); $criteria->compare('create_user_id',$this->create_user_id); $criteria->compare('update_time',$this->update_time,true); $criteria->compare('update_user_id',$this->update_user_id); return new CActiveDataProvider(get_class($this), array( 'criteria'=>$criteria, )); } } Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231494 Share on other sites More sharing options...
freedom55 Posted June 18, 2011 Author Share Posted June 18, 2011 Mr.Adam, yes, I can confirm that the following code works as you said. $model=new Issue; $options = $model->getTypeOptions(); Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231497 Share on other sites More sharing options...
ignace Posted June 18, 2011 Share Posted June 18, 2011 We will need CActiveRecord too. Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231548 Share on other sites More sharing options...
freedom55 Posted June 18, 2011 Author Share Posted June 18, 2011 Yes. That's a good idea. Is it the "PHP getter magic method" and "PHP setter magic method." in this class which are doing the work? I'm attaching the file, as posting exceed character limit. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/239694-question-about-oop-and-magic/#findComment-1231567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.