Yii Framework Forum: Why Use Isset Check For The Propertis That We Sure Is Null - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Why Use Isset Check For The Propertis That We Sure Is Null

#1 User is offline   saman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 23-August 12

Posted 03 August 2013 - 07:57 PM

hi im new to yii, i am reading your book and i have confusing problem below is a peace of the book code in

issueController.php

/**
 * Protected method to load the associated Project model class
 * @project_id the primary identifier of the associated Project
 * @return object the Project data model based on the primary key 
 */
protected function loadProject($projectId)	 
{
	//if the project property is null, create it based on input id
	if($this->_project===null)
	{
		$this->_project=Project::model()->findbyPk($projectId);
		if($this->_project===null)
        	{
			throw new CHttpException(404,'The requested project does not exist.'); 
		}
	}

	return $this->_project; 
}



and my question is why we use

if($this->_project===null)


i think its useless. is it not better to write this? :

/**
 * Protected method to load the associated Project model class
 * @project_id the primary identifier of the associated Project
 * @return object the Project data model based on the primary key 
 */
protected function loadProject($projectId)	 
{
	$this->_project=Project::model()->findbyPk($projectId);
	if($this->_project===null)
        {
		throw new CHttpException(404,'The requested project does not exist.'); 
	}

	return $this->_project; 
} 

0

#2 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 26 August 2013 - 10:00 AM

With your approach you execute a db query every time you call loadProject(), even if you already have one project loaded. The if-clause does nothing to performance, while the sql query could take a long time.
0

#3 User is offline   Yail peralta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-August 13

Posted 30 August 2013 - 03:12 PM

Yeah, Tropi is right. :rolleyes:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users