Strange Error in WebUser

I am not sure if this is a bug or not, but it is strange behavior.

I just upgraded my development machine from PHP 5.1.2 to PHP 5.3. Prior to upgrading, the code for the class that extends CWebUser (from the cookbook tutorial) worked just fine.

Now, using PHP 5.3, I get the following error when I attempt to access any of the attributes of the User model (stored in the database) when not logged into my application:




PHP Error

Description


Trying to get property of non-object


Source File


/my/yii/application/protected/components/WebUser.php(19)


00007:      

00008:     // Return first name.

00009:     // access it by Yii::app()->user->first_name

00010:     public function getFullName()

00011:     {

00012:         $user = $this->loadUser(Yii::app()->user->id);

00013:         return $user->firstName . ' ' . $user->lastName;

00014:     }

00015: 

00016:     public function getName()

00017:     {

00018:         $user = $this->loadUser(Yii::app()->user->id);

00019:         return $user->email;

00020:     }

00021:      

00022:     // Load user model.

00023:     protected function loadUser($id=null)

00024:     {

00025:         if($this->_model===null)

00026:         {

00027:             if($id!==null)

00028:                 $this->_model=User::model()->findByPk($id);

00029:         }

00030:         return $this->_model;

00031:     }



Does anyone know what the cause of this issue could be?

Thanks!

Looks like the error_reporting level in your PHP 5.3 package was changed to include E_STRICT by default. That’s why you now see this error. It was surely there before, but not reported. To fix you should check, that $user!==null before you try to access $user->email.

Either:

a) Yii::app()->user->id == null

or b.) (Yii::app()->user->id != null) && (Yii::app()->user->id is an invalid ID)

You’re both right - thanks!