Understanding $This->Getstate('__Id')===Null;

hi every body

this function


	/**

	 * Returns a value indicating whether the user is a guest (not authenticated).

	 * @return boolean whether the current application user is a guest.

	 */

	public function getIsGuest()

	{

		return $this->getState('__id')===null;

	}




from class CWebUser have a return i couldn’t understand

why ===null as $this->getState(’__id’) will return either a value or a null result?

It’s helper function, Quick and easy way to check if a user is a guest.

If getState is null we’re a guest, if it isn’t we’re a logged in user.




if (Yii::app()->user->isGuest)

{

...

}






if (null === Yii::app()->user->getState('__id'))

{

...

}



When a user is logging in, a call to changeIdentity made. This method saves the id of the user (by default, but can be changed) into session storage.





	/**

     * Changes the current user with the specified identity information.

     * This method is called by {@link login} and {@link restoreFromCookie}

     * when the current user needs to be populated with the corresponding

     * identity information. Derived classes may override this method

     * by retrieving additional user-related information. Make sure the

     * parent implementation is called first.

     * @param mixed $id a unique identifier for the user

     * @param string $name the display name for the user

     * @param array $states identity states

     */

	protected function changeIdentity($id,$name,$states)

	{

		Yii::app()->getSession()->regenerateID();

		$this->setId($id);

		$this->setName($name);

		$this->loadIdentityStates($states);

	}



The usual way to

  • access logged in user information is to call Yii::app()->user->getState(‘fieldname’) and to
  • store info in the session call Yii::app()->user->setState(‘fieldname’, ‘fieldvalue’).

This information is usually set when the user is logging in (and has been verified). Usually, we want to store non private fields that can be accessed later without making a DB call (username, fullname, preferences etc)

That way, when you call getState(’__id’) on a non logged in user, NULL is returned.

Matt

i know but what is the value of adding ===null as getState will return null if not finding a data?

That is true. Just a strong check I guess.





public function getState($key,$defaultValue=null)

	{

		$key=$this->getStateKeyPrefix().$key;

		return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;

	}



i got the answer now

this is a comparison ie: compare that getState is idetical to null & if so the return true;

that’s it

thanks for replay