How to create custom userIdentity class

I want to create custom userIdentity class according to my specific requirements .Here the code is


<?php

    namespace app\models;

    use yii\web\IdentityInterface;

    use app\models\dbTables\Users;

    

    class UserIdentity implements IdentityInterface{

       

       const ERROR_USERNAME_INVALID=3;

       const ERROR_PASSWORD_INVALID=4;

       const ERROR_NONE=0;

       public $errorCode;

       

       private  $_id;

       private  $_email;

       private  $_role;

       private  $_name;

    

       public  function findIdentityById($id){

           $objUserMdl      = new Users;

           $user            = $objUserMdl::findOne($id);

           $userRole        = $objUserMdl->getUserRole($user->user_id);

           $this->_id       = $user->user_id;

           $this->_email    = $user->email_address;

           $this->_role     = $userRole;

           $this->_name     = $user->full_name;

           return $this;

        }

    

        public function getId()

        {

           return $this->_id;

        }

        

        public function getName(){

           return $this->_name;

        }

        

        public function getEmail(){

           return $this->_email;

        }

      

        public function getRole(){

           return $this->_role;

        }

      

        public static function findIdentity($id)

        {

          return self::findIdentityById($id);

        }

        

        public function getAuthKey()

        {

           throw new NotSupportedException('"getAuthKey" is not implemented.');

        }

    

        public function validateAuthKey($authKey)

        {

            throw new NotSupportedException('"validateAuthKey" is not implemented.');

        }

        

        public static function findIdentityByAccessToken($token, $type = null)

        {

            throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');

        }

        

       

        

    }

    

    ?>

Basically I have two tables roles and users and I want to set the specific properties from both table in yii::$app->user->identity

When I call the above code the findIdentity(&#036;id) function returns error for obvious reasons stating that I cannt call &#036;this in static funtion . How can I set the required properties in function and return the instance of userIdentity class from it ?

Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it:


    public static function findIdentity($id)

    {

        $user = new UserIdentity;

        return $user->findIdentityById($id);

    }



Your findIdentityById implementation is not good practice. It is named like a factory method that means it should be static and return an object directly, NOT set instance fields from the find operation.

I say this because when you call "find", it does not logically follow that you are doing anything with that. To use your method, it would read like this:


$user = new UserIdentity();

$user->findIdentityById($someid);

The proper way to use a factory method, is make it static and return a new instance like this:


public static function findIdentityById($id){

           $objUserMdl      = new Users;

           $user            = $objUserMdl::findOne($id);

           $userRole        = $objUserMdl->getUserRole($user->user_id);

           $userId           = new UserIdentity();

           $userId->_id       = $user->user_id;

           $userId->_email    = $user->email_address;

           $userId->_role     = $userRole;

           $userId->_name     = $user->full_name;

           return $userId;

        }

// Somewhere else

$user = UserIdentity::findIdentityById($someid);

This also allows you to call it properly from findIdentity()